PHP mktime 和时区

PHP mktime and timezone

我正在研究一些与时间相关的功能,我选择始终使用 UTC 时间并将时间戳存储为整数以保持一致性。

但是,我注意到当我使用mktime时,当前设置的时区似乎对mktime的return值有影响。从文档中我了解到 mktime 应该是 return 自纪元以来的秒数:

Returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

http://php.net/manual/en/function.mktime.php

但是,mktime 似乎包括当前设置的时区。使用以下代码时:

date_default_timezone_set('UTC');
$time = mktime(0, 0, 0, 1, 1, 2016 );
echo "{$time}\n";

date_default_timezone_set('Australia/Sydney');
$time = mktime(0, 0, 0, 1, 1, 2016 );
echo "{$time}\n";

我希望这两个时间值相同,但显然它们不是:

1451606400
1451566800

这似乎正好是 11 小时的时差:

1451606400 - 1451566800 = 39600 / (60*60) = 11

我对 mktime 有什么理解不正确 and/or 为什么在使用 mktime 时要考虑时区?

我不能告诉你为什么会这样(PHP 在日期和时间方面对我来说从来没有意义)但是有一个替代函数 gmmktime()

Identical to mktime() except the passed parameters represents a GMT date. gmmktime() internally uses mktime() so only times valid in derived local time can be used.

还有一个 comment on the PHP documentation for this function 解释了 mktime()gmmktime()time() 的工作原理。从本质上讲,他们假设您总是在时区中思考,即使 UNIX 时间戳本身不带有时区。

生成的 Unix 时间戳确实以时区不可知的方式编码,但输入参数是相对于当前进程设置的时区进行解释的。事实上,Sidneys 2016-01-01 00:00:00 (GMT+11) 比 UTC 2016-01-01 00:00:00 早 11 小时。 当一些外国人告诉你一个时间时,你必须知道它的时区才能正确解释它,mktime()也是如此。

如果您要传递给 mktime() 的日期是 UTC 日期,则使用为此目的而存在的 gmmktime()