setTimestamp(DateT-6ren">
gpt4 book ai didi

PHP:时区数据是否以某种方式嵌入到时间戳中?

转载 作者:可可西里 更新时间:2023-11-01 01:09:18 26 4
gpt4 key购买 nike

date_default_timezone_set("Asia/Singapore"); // UTC +8
$dt = new DateTime();
$dt->setTimestamp(DateTime::createFromFormat('u', gmdate('u'))->getTimestamp());
echo $dt->getTimezone()->getName(); // Asia/Singapore
echo $dt->format('d M Y H:i:s'); // Correct local time

$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('d M Y H:i:s'); // Correct UTC Time
die;

我想知道时间戳是否包含时区数据。在第 3 行,您看到我使用了 gmdate(),它应该给我 UTC/GMT 时间。但是当我得到时区和格式化的日期时间时,它们是在本地时间。我还没有在我的 DateTime 对象中设置时区,给它一个 UTC 时间戳。它以某种方式知道转换为本地时间。这让我想知道时区数据是否包含在时间戳中

最佳答案

setTimestamp 接受 GMT 0 中的时间戳,并且您在 GMT 中传递它,因为 gmdate('u')DateTime 对象默认采用您当前的时区。

之后 - 您已正确设置时间戳和当前时区,这就是 DateTime 对象格式化新加坡日期的原因。

Which makes me wonder if Timezone data are included in timestamps

没有。时间戳仅存储自 unix 纪元以来的秒数。

关于PHP:时区数据是否以某种方式嵌入到时间戳中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4766599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com