Server strtotime incorrect

2019-08-03 18:20发布

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条回答
贼婆χ
2楼-- · 2019-08-03 18:37

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.

查看更多
登录 后发表回答