DateTime :: modify和DST切换 [英] DateTime::modify and DST switch

查看:57
本文介绍了DateTime :: modify和DST切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 DateTime :: modify 在DST边界上加一个小时会导致跳过一个小时.

Using DateTime::modify to add an hour across a DST boundary causes it to skip an hour.

例如

$d = new DateTime('2015-11-01 12:00:00 AM', new DateTimeZone('America/Vancouver'));
$d->modify('+1 hour'); // 1 AM
$d->modify('+1 hour'); // 2 AM
$d->modify('+1 hour'); // 3 AM

我想两次看到凌晨1点"(然后是然后凌晨2点"),因为时间回到了一个小时.

I want to see "1 AM" twice (and then "2 AM") because time goes back an hour.

我如何获得这种行为?

推荐答案

它是一个错误.(信用)

要解决此问题,请将时区更改为UTC,然后再次返回.

To work around it, change the timezone to UTC and then back again.

$d = new DateTime('2015-11-01 12:00:00 AM', new DateTimeZone('America/Vancouver'));

$tz = getTimezone();
$d->setTimezone(new DateTimeZone('UTC'));
$d->modify('+1 hour'); 
$d->modify('+1 hour'); 
$d->modify('+1 hour'); 
$d->setTimezone($tz);
echo $d->format('d-M-Y g:ia'); // 01-Nov-2015 2:00am

这篇关于DateTime :: modify和DST切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆