1

我对我制作的脚本感到非常沮丧,我在其中比较了时间戳。我可以看到时间戳完全不同。

  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);
4

3 回答 3

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

于 2012-10-02T15:12:35.930 回答
2

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

SELECT UNIX_TIMESTAMP() AS 'Current' UNIX_TIMESTAMP(`datefield`) AS 'Till' FROM `table`
于 2012-10-02T15:22:30.507 回答
1

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

代码:

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

于 2012-10-02T15:14:03.280 回答