PHP strtotime 和 MySQL UNIX_TIMESTAMP 返回不同的整数
echo strtotime('2010-09-11 12:15:01');
返回:1284207301
SELECT UNIX_TIMESTAMP('2010-09-11 12:15:01')
返回:1284200101
为什么有差异?
echo strtotime('2010-09-11 12:15:01');
Returns: 1284207301
SELECT UNIX_TIMESTAMP('2010-09-11 12:15:01')
Returns: 1284200101
Why the difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
答案就在 php.net 关于 strtotime 的文章中:
简短版本:strtotime 默认使用您的时区,UNIX_TIMESTAMP 则不使用。
The answer is in the php.net article about strtotime:
Short version: strtotime uses your timezone by default, UNIX_TIMESTAMP does not.
这通常是由于将 PHP 和 MySQL 设置为使用不同的时区,以及如果它们恰好位于不同的服务器上,则系统时间存在差异。
UNIX 时间戳是自 UNIX 纪元(UTC 时间 1970 年 1 月 1 日午夜)以来经过的秒数,因此时区差异将给出不同的时间戳。
This is commonly caused by setting PHP and MySQL to use different timezones, as well as discrepancies in the system time if they happen to be located in different servers.
UNIX timestamps are the number of seconds that have passed since the UNIX epoch on midnight January 1, 1970 UTC so timezone differences will give different timestamps.
1284207301-1284200101 = 7200
-> 2小时。如此完美的差异并不是由于错误,而是时区差异。很可能一个是 UTC/GMT,另一个是 UTC +/- 21284207301-1284200101 = 7200
-> 2 hours. Such a perfectly rounded difference isn't due to a bug, it's a timezone diference. Most likely one's in UTC/GMT, and the other's UTC +/- 2我的猜测是 Unix 时间是 GMT,另一个是你当地的时区,两个输出之间正好有 2 小时的差异 (7200/60/60)
My guess is that Unix time is GMT and the other is your local timezone, there is exactly 2 hours difference (7200/60/60) between the 2 outputs