我有两个以JSON返回的数据:
"startTimeUTC":"2018-07-03T21:00:00.000Z"
"siteTimeZone":-4.0"
然后我要在PHP中这样做:
$start = date_format(date_create($event['startTimeUTC']), 'Y-m-d h:i:s');
哪个返回此:
2018-07-03 09:00:00
我不确定如何从原始时间中减去时区偏移量。
预期时间应该是:05:00:00 PM
我已经尝试了很多不同的方法来使它起作用,但是没有运气。
如何根据时区偏移量获取正确的时间以转换为本地时间?
答案 0 :(得分:4)
日期字符串已经包含Zulu
时区。因此,您需要创建一个新的DateTime
对象,然后再设置新的时区。
$date = new Datetime('2018-07-03T21:00:00.000Z');
$date->setTimezone(new DateTimeZone('-4.0'));
var_dump($date->format('Y-m-d H:i:s')); // 2018-07-03 17:00:00
答案 1 :(得分:0)
您可以将时区用作乘数,以从UTC时间加/减。
// convert your time via `strtotime` and then add your offset * 3600 (seconds in an hour)
$start = date('Y-m-d h:i:s',strtotime($event['startTimeUTC']) + ($event['siteTimeZone'] * 3600)) ;