简体   繁体   English

PHP strtotime和MySQL UNIX_TIMESTAMP返回不同的整数

[英]PHP strtotime and MySQL UNIX_TIMESTAMP return different integer

echo strtotime('2010-09-11 12:15:01');

Returns: 1284207301 返回值:1284207301

SELECT UNIX_TIMESTAMP('2010-09-11 12:15:01')

Returns: 1284200101 回报:1284200101

Why the difference? 为什么会有所不同?

The answer is in the php.net article about strtotime : 答案在有关strtotime的php.net 文章中

This function will use the TZ environment variable (if available) to calculate the timestamp. 此函数将使用TZ环境变量(如果有)来计算时间戳。 Since PHP 5.1.0 there are easier ways to define the timezone that is used across all date/time functions. 从PHP 5.1.0开始,有更简便的方法来定义在所有日期/时间函数中使用的时区。 That process is explained in the date_default_timezone_get() function page. 该过程在date_default_timezone_get()函数页面中进行了说明。

Short version: strtotime uses your timezone by default, UNIX_TIMESTAMP does not. 简短版本:strtotime默认使用您的时区,而UNIX_TIMESTAMP则不使用。

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. 这通常是由于将PHP和MySQL设置为使用不同的时区以及系统时间差异(如果它们恰好位于不同的服务器上)而引起的。

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. UNIX时间戳是自UTC时间1970年1月1日午夜以来的秒数,因此时区差异将给出不同的时间戳。

1284207301-1284200101 = 7200 -> 2 hours. 1284207301-1284200101 = 7200 > 2小时。 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 最有可能是UTC / GMT,另一个是UTC +/- 2

我的猜测是Unix时间是格林尼治标准时间(GMT),另一个是您的本地时区,两个输出之间恰好相差2小时(7200/60/60)

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

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