繁体   English   中英

PHP时间戳和mysql时间戳的区别

[英]Difference in PHP timestamp and mysql timestamp

我对自己编写的脚本感到非常不满,我在其中比较了时间戳。 我可以看到时间戳完全不同。

  1. 对于2012-10-02 03:06:21,时间戳为:1349190381
  2. 对于2012-10-02 04:00:50,时间戳为:1349150450

我不明白为什么第二个时间戳比第一个时间戳低,即使时间更长。 如何比较两个日期?

我正在通过strtotime()函数获取这些值。

澄清 :我正在使用

$current_timestamp = strtotime(date('Y-m-d H:i:s'));

$till_date_timestamp = strtotime($database_object->till_date);

你确定你做对了吗?

mysql> select from_unixtime(1349190381), from_unixtime(1349150450);
+---------------------------+---------------------------+
| from_unixtime(1349190381) | from_unixtime(1349150450) |
+---------------------------+---------------------------+
| 2012-10-02 09:06:21       | 2012-10-01 22:00:50       |
+---------------------------+---------------------------+
1 row in set (0.00 sec)

php > echo date('r', 1349190381), "\n", date('r', 1349150450);
Tue, 02 Oct 2012 10:06:21 -0500
Mon, 01 Oct 2012 23:00:50 -0500

您在strtotime通话中使用了什么字符串? 该功能可以误译投入,不应该对任何东西,但良好且明确的日期字符串信任。

不要依赖strtotime 同样很明显,PHP的时区与数据库的时区不同。 确保时区相同,或者像这样获取当前/截止时间戳(以使它们来自一个来源):

SELECT UNIX_TIMESTAMP() AS 'Current' UNIX_TIMESTAMP(`datefield`) AS 'Till' FROM `table`

这是我得到的结果 它们不同于您在问题中提到的内容。

码:

    echo strtotime('2012-10-02 03:06:21'); //outputs: 1349147181
    echo "\n";
    echo strtotime('2012-10-02 04:00:50'); //outputs: 1349150450

编辑:

$till_date_timestamp = strtotime($database_object->till_date); 确保$database_object->till_date是一个字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM