我很难使用PHP DateTime将GMT -8时区(PST)接收的日期转换为带有时区GMT -7 (PDT)的人类可读的格式。
下面是一个例子:
$tz = new DateTimeZone('America/Los_Angeles');
$saleEndDate = new DateTime("2016-11-07T17:30:00-08:00");
$saleEndDate->setTimezone($tz);
echo $saleEndDate->format('Y-m-d H:i:s');以上代码的输出是:2016-11-07 17:30.然而,它应该显示2016-11-07 18:30,因为America/Los_Angeles现在在日光节约(GMT -7,PDT)。
根据我在DateTime文档中看到的内容,new DateTime命令应该能够解释字符串2016-11-07T17:30:00-08:00有一个GMT -8时区:
当时间参数包含时间戳(例如946684800)或指定时区(例如10-01-28T15:00:00+02:00)时,时区参数和当前时区将被忽略。
即使如此,我认为DateTime并没有正确地识别GMT -8 .
有谁知道在时区之间正确转换所需的方法吗?
更新:
我还尝试将DateTimeZone作为第二个参数传入DateTime构造函数,但也没有效果:
$tz = new DateTimeZone('America/Los_Angeles');
$saleEndDate = new DateTime("2016-11-07T17:30:00-08:00", new DateTimeZone("America/Los_Angeles"));
$saleEndDate->setTimezone($tz);
echo $saleEndDate->format('Y-m-d H:i:s');也不起作用:
$tz = new DateTimeZone('America/Los_Angeles');
$saleEndDate = new DateTime("2016-11-07T17:30:00", new DateTimeZone("PST"));
$saleEndDate->setTimezone($tz);
echo $saleEndDate->format('Y-m-d H:i:s');也不起作用:
$tz = new DateTimeZone("PDT");
$saleEndDate = new DateTime("2016-11-07T17:30:00", new DateTimeZone("PST"));
$saleEndDate->setTimezone($tz);
echo $saleEndDate->format('Y-m-d H:i:s');发布于 2016-09-28 02:44:29
不是最棒的,但这是我唯一能想到的办法
$tz = new DateTimeZone('America/Los_Angeles');
$saleEndDate = new DateTime("2016-11-07T17:30:00-08:00");
$saleEndDate->setTimezone($tz);
$stamp = $saleEndDate->format('U');
$zone = $tz->getTransitions($stamp, $stamp);
if(!$zone[0]['isdst']) $saleEndDate->modify('+1 hour');
echo $saleEndDate->format('Y-m-d H:i:s');我在这里所做的是使用DateTimeZone::getTransitions函数来确定您提供的日期是否为DST。如果不是,我们再加一个小时。注意,这并不改变时区,它只是校正DST移位。
发布于 2017-09-28 00:52:48
DateTime正在发挥它应有的作用。除非你是在一个地区观察到不同的DST与大面积,America/Los_Angeles离开DST (PDT->PST)在11月6日(在2016年)。
https://www.timeanddate.com/news/time/usa-canada-end-dst-2016.html
在timezeonedb中,您可以通过搜索数组中的特定日期/时间来检查它使用的日期(麦查利曾经这样做过),得到了它不在DST中的事实,然后继续手动修改它。这不是一个答案,因为它最终将失败,除非您手动添加一个切断时间,手动更正停止。
检查您的日期周围的过渡日期会发现:
date_default_timezone_set('America/Los_Angeles');
$theDate = new DateTime("2016-11-07T17:30:00",new DateTimeZone("America/Los_Angeles"));
$theDateBefore = new DateTime("2016-03-01");
$theDateAfter = new DateTime("2017-03-15");
echo "<pre>";
print_r( $theDate->getTimezone()->getTransitions(
$theDateBefore->getTimestamp(),$theDateAfter->getTimestamp()));
echo "</pre>";产生的数组为4:
Array
(
[0] => Array
(
[ts] => 1456819200
[time] => 2016-03-01T08:00:00+0000
[offset] => -28800
[isdst] =>
[abbr] => PST
)
[1] => Array
(
[ts] => 1457863200
[time] => 2016-03-13T10:00:00+0000
[offset] => -25200
[isdst] => 1
[abbr] => PDT
)
[2] => Array
(
[ts] => 1478422800
[time] => 2016-11-06T09:00:00+0000
[offset] => -28800
[isdst] =>
[abbr] => PST
)
[3] => Array
(
[ts] => 1489312800
[time] => 2017-03-12T10:00:00+0000
[offset] => -25200
[isdst] => 1
[abbr] => PDT
)
)数组是与theDateBefore一样有效的时区,您可以看到日期更改对您的时间序列有效。
您的销售日期后,从PDT到PST的变化。
要让代码返回经过调整的日期/时间,您需要手动更改它。按照已经被接受的方式去做会产生错误的结果。正如我提到的,您需要用您想要强制执行自定义时区的日期来包围它。
https://stackoverflow.com/questions/39736509
复制相似问题