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

时间:2012-10-02 15:09:36

标签: php mysql date

我对我制作的脚本感到很不安,我在其中对时间戳进行了比较。 我可以看到时间戳完全不同。

  1. 2012-10-02 03:06:21,时间戳是:1349190381
  2. 2012-10-02 04:00:50,时间戳是:1349150450
  3. 我无法理解为什么第二个时间戳低于第一个时间戳,即使时间更长。我如何比较两个日期?

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

    澄清:我正在使用

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

3 个答案:

答案 0 :(得分:2)

你确定你做得对吗?

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电话中使用了什么字符串?函数 CAN 误解了输入,除了格式正确且明确的日期字符串之外,不应该信任任何内容。

答案 1 :(得分:2)

不要依赖strtotime!显而易见,PHP的时区与数据库的时区不同。确保时区相同或获得当前/直到这样的时间戳(以便它们来自一个来源):

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

答案 2 :(得分:1)

这是我得到的result。它们与您在问题中提到的不同。

代码:

    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是一个字符串。