如何对时间戳进行 strtotime,或者如何最好地处理 SQL 日期?

发布于 2024-10-11 15:12:38 字数 902 浏览 9 评论 0 原文

在我的 PHP 应用程序中,当我使用 PHP 的 date() 命令格式化日期时,我从 MySQL 数据库获取日期。我想整理一下并将其完全移至视图,而不是检索。但我遇到了一些问题。

从数据库获取日期时,我在对其运行 strtotime() 后将其传递给应用程序的其余部分,以便 date() 函数拥有它们可以使用的东西与. 一起工作。这是个好主意吗?或者我错过了一些更好和明显的东西?我正在处理的日期几乎都是 DATE 类型,而不是 DATETIME 类型。

这样,我就得到了日期,并且可以使用 date() 将其显示在网页上。但有时,我会从数据库中获取一些内容,然后将其插入数据库中的其他位置,并且必须在后端中寻找需要 date() 的位置,这违背了将其分离出来的目的到视图。我的检索函数有助于返回 strtotime($date_from_db),然后我在另一个表中创建新条目...并且所有新条目都必须通过验证和准备函数。问题就在这里。我通过查看 strtotime() 是否为 0000-00-00 以外的值来验证和准备日期。但我无法在时间戳上运行 strtotime();在我运行的测试中,如果给了我 12/31/1969。所以我的问题是:

  1. 是否可以以某种方式在时间戳上运行 strtotime()
  2. 如果不是,是否有一种简单的方法可以知道变量是否是时间戳?所以我可以简单地运行 date() 而不是处理 strtotime() 吗?除了看看它是否会在 1969 年 12 月 31 日发布之外,因为我不知道我是否可以相信失败会保持一致。除非那是一件有效的事情?
  3. 我这样做完全错误吗?

In my PHP app, I get dates from the MySQL database, when I format them with PHP's date() command. I want to tidy that up and move it all entirely to the view, rather than the retrieval. But I ran into some problems.

When getting a date from the database, I pass it to the rest of the app after running strtotime() on it, so that the date() functions have something they can work with. Is this a good idea? or am I missing something better and obvious? The dates that I'm dealing with are nearly all in the DATE type, not DATETIME.

So, I get the date, and can use date() to display it on the webpage. But sometimes, I'll get something from the database solely to insert elsewhere in the database, and having to hunt through the backend to find places that need date() kind of defeats the purpose of separating it out to the view. My retrieval functions helpfully return strtotime($date_from_db), and then I create the new entry into the other table... and all new entries must pass through the validation and preparation functions. And herein lies the problem. I validate and prepare dates by seeing if they strtotime() to something other than 0000-00-00. But I can't run strtotime() on a timestamp; in the test I ran, if gave me 12/31/1969. So my questions:

  1. Is it possible somehow to run strtotime() on a timestamp?
  2. If not, is there an easy way to know if a variable is a timestamp? So I could simply run date() on it rather than dealing with strtotime()? Apart from seeing if it comes out to 12/31/1969, because I don't know if I can trust a failure to be consistent. Unless that's a valid thing to do anyway?
  3. And am I doing this completely wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

锦上情书 2024-10-18 15:12:38

作为一般方法,我很想:

  1. 使用 MySQL 的日期时间类型存储日期/时间值,因为这将允许您有意义地查询 MySQL 中的数据,而不必求助于使用 >= "timestampvalueX"等。

  2. 保留所有 "在 PHP 脚本中使用时间戳格式的“实时”日期信息。

因此,您可能希望以该格式存储所有与日期/时间相关的信息作为目标。您可以轻松地将其转换为 PHP 可以在查询本身中使用的格式(使用 UNIX_TIMESTAMP) 或通过分解 YYYY-MM-DD HH:MM:SS 格式并使用 PHP 的 mktime 创建“标准”时间戳。 (尽管您可能希望使用 checkdate 进行验证。

)针对您的具体问题:

  1. 如果通过时间戳,strotime 将失败。

  2. 作为临时措施,您可以编写一个函数来检查提供的值是否为 10 位整数,如果是,则假定它是本机时间戳。 (if(is_int($value) && strlen($value) == 10)...)

  3. 听上去有点混乱,所以作为一个合理的短期目标,您可能应该尝试标准化 MySQL 和 PHP 中的使用。

As a general approach, I'd be very tempted to :

  1. Store date/time values using MySQL's datetime type, as this will allow you to meaningfully query the data within MySQL without having resort to either using >= "timestampvalueX", etc.

  2. Keep all "live" date information using within PHP scripts in timestamp format.

As such, you probably want to aim to store all of the date/time related information in that format as a goal. You can the trivially convert this into a format PHP can use either in the query itself (using UNIX_TIMESTAMP) or by exploding the YYYY-MM-DD HH:MM:SS format and using PHP's mktime to create a "standard" timestamp. (Although you might want to use checkdate for validation purposes.)

Getting back to your specific questions:

  1. strotime will fail if a timestamp is passed.

  2. As an interim measure you could write a function that checked if the provided value was a 10 digit integer, and presume that it was a native timestamp if it was. (if(is_int($value) && strlen($value) == 10)...)

  3. It's a bit of a mess by the sounds of things, so as a reasonably short term goal you should probably try and standardise usage both in MySQL and PHP.

ゞ花落谁相伴 2024-10-18 15:12:38

当你查询时间戳时让mySQL来处理它。

http://dev.mysql .com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

该查询将检索您需要的格式化时间戳,从而使您无需对字符串进行任何处理。

Let mySQL handle it when you query the timestamp.

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

The query will retrieve the timestamp formatted as you need it saving you from having to do any processing the string.

请恋爱 2024-10-18 15:12:38

但是我无法在时间戳上运行 strtotime() ;在我运行的测试中,如果给了我 12/31/1969。

12/31/1969 是 UNIX 纪元 1-1-1970 的开始,带有时区偏移。

对时间戳运行 strtotime() 将返回 0,当您通过 date() 运行它时,最终结果为 1-1-1970 UTC。检查 strtotime 结果是否为 0 就可以了。

But I can't run strtotime() on a timestamp; in the test I ran, if gave me 12/31/1969.

12/31/1969 is the start of the UNIX epoch, 1-1-1970, with a timezone offset.

Running strtotime() on a timestamp will return 0, which winds up as 1-1-1970 UTC when you run it through date(). Checking for a strtotime result of 0 should do it.

白芷 2024-10-18 15:12:38

简而言之,在从数据库返回的时间戳前面添加一个“@”符号,您就可以像普通时间戳一样使用它。建议将此解决方案作为一种尽可能减少与提问者熟悉的技术和方法的分歧的方法(以最小的努力/混乱)。

您的数据库返回时间戳的一个版本,它是一个字符串,而 PHP 时间戳是一个时间戳对象。

使用“@”技巧,您可以一致地编码。无论您是处理 PHP 中的时间戳对象还是 MYSQL 返回的字符串,这并不重要。

$ts1 = time();       //a PHP timestamp object
$ts2 = '1628100056'; //a string, like one coming back from a timestamp field in a MYSQL DB

//see here how they are both treated the same...
$date1 = date('Y-m-d',strtotime('@'.$ts1));
$date2 = date('Y-m-d',strtotime('@'.$ts2));

echo "from object: ".$date1;
echo "<BR>";
echo "from string: ".$date2;

应从时间戳字段检索来自数据库的时间戳,如下所示:

SELECT UNIX_TIMESTAMP(my_timestamp_field) FROM my_table;

In short, prepend an '@' symbol to your timestamp returned from your database and you can use it in the same way as normal timestamp. This solution is suggested as a means of diverging as minimally as possible from the techniques and approaches which the questioner is familiar with (for minimal effort/confusion).

your db returns a version of a timestamp which is a string, whereas a PHP timestamp is a timestamp object.

with the '@' trick, you can code consistently. it doesn't matter whether you're processing a timestamp object from PHP or a string returned by MYSQL.

$ts1 = time();       //a PHP timestamp object
$ts2 = '1628100056'; //a string, like one coming back from a timestamp field in a MYSQL DB

//see here how they are both treated the same...
$date1 = date('Y-m-d',strtotime('@'.$ts1));
$date2 = date('Y-m-d',strtotime('@'.$ts2));

echo "from object: ".$date1;
echo "<BR>";
echo "from string: ".$date2;

the timestamp from the DB should be retrieved from the timestamp field thus:

SELECT UNIX_TIMESTAMP(my_timestamp_field) FROM my_table;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文