Server strtotime incorrect

时光毁灭记忆、已成空白 提交于 2020-01-06 02:14:37

问题


Both strtotime and mktime are outputting an incorrect timestamp and it's driving me mad.

If I add the following strtotime('2012-10-09');

I get 1349701200

Which is actually Mon, 08 Oct 2012 13:00:00 GMT

I'm using my localhost, running MAMP. I'm assuming it's a server timezone issue, or something, but I don't understand why, or how to fix it.

Any help?


回答1:


strtotime uses default timezone to interpret the string. If you want different timezone you could specify it explicitly or change it for all calls:

<?php

if (date_default_timezone_get()) {
    echo  'date_default_timezone:  ' . date_default_timezone_get()."\n";
}

echo strtotime('2012-10-09')."\n"; # default timezone
echo strtotime('2012-10-09 UTC')."\n";
date_default_timezone_set('UTC');
echo strtotime('2012-10-09')."\n";
?>

Output

date_default_timezone:  Europe/London
1349737200
1349740800
1349740800

POSIX timestamp counts number of seconds since 1970-01-01 00:00:00 UTC. For example, midnight (00:00) in New York may be 20:00 in UTC at this time of year (the same POSIX timestamp). But 00:00 in UTC and 00:00 in New York correspond to different moments in time (different POSIX timestamps). Local clocks follow the Sun usually (roughly speaking) and even if it is night where you are; the Sun shines somewhere on Earth.



来源:https://stackoverflow.com/questions/12985413/server-strtotime-incorrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!