简体   繁体   中英

PHP strtotime and MySQL UNIX_TIMESTAMP return different integer

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

Returns: 1284207301

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

Returns: 1284200101

Why the difference?

The answer is in the php.net article about strtotime :

This function will use the TZ environment variable (if available) to calculate the timestamp. Since PHP 5.1.0 there are easier ways to define the timezone that is used across all date/time functions. That process is explained in the date_default_timezone_get() function page.

Short version: strtotime uses your timezone by default, UNIX_TIMESTAMP does not.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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