不同时区的时间戳相同吗?

发布于 2024-12-25 09:36:12 字数 733 浏览 2 评论 0原文

我运行了这个脚本:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘味果▽酱 2025-01-01 09:36:12

是的,它们都使用 UTC。具体来说,time()U 格式都返回自 1970 年 1 月 1 日午夜 UTC(“Unix 纪元”)以来经过的非闰秒数。由于纪元是一个固定点,其定义与用户的时区无关,并且从那时起的秒数在所有时区中都是相同的(忽略相对论效应等),time() 返回无论时区设置如何,都具有相同的值。

Yes, they both use UTC. Specifically, both time() and the U 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.

无所的.畏惧 2025-01-01 09:36:12

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文