dsjzmrz78089 2012-01-07 03:11
浏览 1005
已采纳

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

I ran this script:

echo date_default_timezone_get()."
";
echo "----
";

date_default_timezone_set('Asia/Kabul');
echo date_default_timezone_get()."
";
echo "----
";

echo time()."
";
$dt = new DateTime;
echo $dt->format('U') . "
";
echo "----
";

date_default_timezone_set('UTC');
echo date_default_timezone_get()."
";
echo "----
";

echo time()."
";
$dt = new DateTime;
echo $dt->format('U') . "
";

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??

  • 写回答

2条回答 默认 最新

  • dqmnueuu459279 2012-01-07 03:16
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP