商业澳洲 文章 php获取悉尼时间,php在使用澳大利亚/悉尼时区时给出错误答案

php获取悉尼时间,php在使用澳大利亚/悉尼时区时给出错误答案

为什么它说5,为什么这在技术上是正确的

在Sydney,DST开始于2013-10-06 02:00:00 – 所以你跨越那个日期会失去一个小时.

当你调用strtime时,它会将时间解释为悉尼时间,但返回一个Unix时间戳.如果您将第二组时间戳转换为UTC,则会得到2013-09-30 14:00:00到2013-10-06 13:00:00的范围,这不是6天,所以得到向下舍入到5.

如何获得忽略DST转换的时差

请尝试使用DateTime对象,例如

$tz=new DateTimeZone('Australia/Sydney');

$start=new DateTime('2013-10-01', $tz);

$end=new DateTime('2013-10-07', $tz);

$diff=$end->diff($start);

//displays 6

echo "difference in days is ".$diff->d."\n";

为什么DateTime :: diff的工作方式不同?

你可能会问“为什么会这样?” – 毕竟,在这些时间之间确实没有6天,它是5天23小时.

原因是DateTime::diff实际上纠正了DST转换.我不得不阅读源代码来解决这个问题 – 修正发生在内部timelib_diff函数中.如果满足以下所有条件,则会发生此更正

>每个DateTime使用相同的时区

>时区必须是地理ID,而不是GMT这样的缩写

>每个DateTime必须具有不同的DST偏移(即一个在DST中,一个不在DST中)

为了说明这一点,如果我们在切换到夏令时的任何一侧使用两次几个小时就会发生这种情况

$tz=new DateTimeZone('Australia/Sydney');

$start=new DateTime('2013-10-06 00:00:00', $tz);

$end=new DateTime('2013-10-06 04:00:00', $tz);

//diff will correct for the DST transition

$diffApparent=$end->diff($start);

//but timestamps represent the reality

$diffActual=($end->getTimestamp() – $start->getTimestamp()) / 3600;

echo "Apparent difference is {$diffApparent->h} hours\n";

echo "Actual difference is {$diffActual} hours\n";

这输出

Apparent difference is 4 hours

Actual difference is 3 hours

原文链接:https://blog.csdn.net/weixin_39645165/article/details/116057759?ops_request_misc=&request_id=7a0d77931a5e4938a558e149da598e1f&biz_id=&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~koosearch~default-26-116057759-null-null.268%5Ev1%5Econtrol&utm_term=%E6%BE%B3%E6%B4%B2%E7%94%9F%E6%B4%BB

作者: 知澳头条

知澳资深作者

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

联系我们

联系我们

1300766331

邮箱: info@getau.com.au

澳洲本地网站设计开发团队 超20人团队,悉尼设计开发14年 联系电话:1300766331 微信: XtechnologyAU
关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部