代码之家  ›  专栏  ›  技术社区  ›  user1547766

PHP时间戳和mysql时间戳的差异

  •  1
  • user1547766  · 技术社区  · 13 年前

    我对我制作的剧本感到非常不安,我在剧本中对时间戳进行了比较。 我可以看出时间戳是完全不同的。

    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);
    
    3 回复  |  直到 13 年前
        1
  •  2
  •   Marc B    13 年前

    你确定你做得对吗?

    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调用中使用了什么字符串?功能 罐头 错误地解释了输入,除了格式良好且明确的日期字符串之外,不应信任其他任何内容。

        2
  •  2
  •   ddinchev    13 年前

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

    SELECT UNIX_TIMESTAMP() AS 'Current' UNIX_TIMESTAMP(`datefield`) AS 'Till' FROM `table`
    
        3
  •  1
  •   Teena Thomas    13 年前

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