简体   繁体   English

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

[英]Difference in PHP timestamp and mysql timestamp

I am really upset with the script I made, in which I did comparison of timestamps. 我对自己编写的脚本感到非常不满,我在其中比较了时间戳。 I can see that the timestamps are totally different. 我可以看到时间戳完全不同。

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

I can't understand why the second one timestamp is lower than the first, even if the time is bigger. 我不明白为什么第二个时间戳比第一个时间戳低,即使时间更长。 How can I compare two dates? 如何比较两个日期?

I am getting these values by strtotime() function. 我正在通过strtotime()函数获取这些值。

Clarification : I am using 澄清 :我正在使用

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

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

You sure you did it right? 你确定你做对了吗?

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

what string did you use in your strtotime calls? 您在strtotime通话中使用了什么字符串? The function CAN mis-interpret inputs and should not be trusted on anything but well-formed and unambiguous date strings. 该功能可以误译投入,不应该对任何东西,但良好且明确的日期字符串信任。

Don't rely on strtotime ! 不要依赖strtotime Also it's obvious that the timezone of PHP is different then the timezone of the database. 同样很明显,PHP的时区与数据库的时区不同。 Make sure the timezones are the same or get current/till timestamp like this (so that they are from one source): 确保时区相同,或者像这样获取当前/截止时间戳(以使它们来自一个来源):

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

this is the result that i got. 这是我得到的结果 They differ from what you mentioned in the question. 它们不同于您在问题中提到的内容。

Code: 码:

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

Edit: 编辑:

$till_date_timestamp = strtotime($database_object->till_date); make sure that $database_object->till_date is a string. 确保$database_object->till_date是一个字符串。

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

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