不同时区的时间戳相同吗?
我运行了这个脚本:
echo date_default_timezone_get()."\n";
echo "----\n";
date_default_timezone_set('Asia/Kabul');
echo date_default_timezone_get()."\n";
echo "----\n";
echo time()."\n";
$dt = new DateTime;
echo $dt->format('U') . "\n";
echo "----\n";
date_default_timezone_set('UTC');
echo date_default_timezone_get()."\n";
echo "----\n";
echo time()."\n";
$dt = new DateTime;
echo $dt->format('U') . "\n";
对于所有 4 个时间戳值,它显示:1325905766
。
我在某处读到,无论时区设置如何,新的 DateTime 总是将 UTC 设置为时区,所以我明白了,但是 time()
呢?
几秒钟前,在另一台服务器上(可能不在欧洲 (ideone.com)),它返回 4 次:1325905723
。
那是一样的! (明显有几秒的差异,但时区相同。)
time()
始终且仅返回 UTC???这是怎么回事??
I ran this script:
echo date_default_timezone_get()."\n";
echo "----\n";
date_default_timezone_set('Asia/Kabul');
echo date_default_timezone_get()."\n";
echo "----\n";
echo time()."\n";
$dt = new DateTime;
echo $dt->format('U') . "\n";
echo "----\n";
date_default_timezone_set('UTC');
echo date_default_timezone_get()."\n";
echo "----\n";
echo time()."\n";
$dt = new DateTime;
echo $dt->format('U') . "\n";
and for all the 4 timestamp values, it says: 1325905766
.
I read somewhere that new DateTime always sets UTC as timezone, whatever the timezone setting, so I get that, but what about time()
?
A few seconds before, on a different server (probably not in Europe (ideone.com)), it returns 4 times: 1325905723
.
That's the same! (A few seconds diff obviously, but the same timezone.)
time()
always and only returns UTC??? What's going on??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,它们都使用 UTC。具体来说,
time()
和U
格式都返回自 1970 年 1 月 1 日午夜 UTC(“Unix 纪元”)以来经过的非闰秒数。由于纪元是一个固定点,其定义与用户的时区无关,并且从那时起的秒数在所有时区中都是相同的(忽略相对论效应等),time()
返回无论时区设置如何,都具有相同的值。Yes, they both use UTC. Specifically, both
time()
and theU
format return the number of non-leap seconds elapsed since midnight UTC on January 1, 1970 (the "Unix epoch"). As the epoch is a fixed point whose definition is independent of the user's timezone, and as the number of seconds since then is the same in all time zones (ignoring relativistic effects and whatnot),time()
returns the same value regardless of time zone settings.time()
返回自一月以来的秒数1970 年 1 月 1 日 00:00:00 GMT。无论您是在 Asia/Kabul 还是 Hither/Yon,自该特定时间点以来经过的秒数都是相同的。
time()
returns the number of seconds since January 1st, 1970 00:00:00 GMT.The number of seconds elapsed since that particular point in time is the same whether you are in Asia/Kabul or Hither/Yon.