我正在尝试获取一个ISO 8601格式的Zulu时间字符串和一个时区字符串的本地时间。我阅读了PHP文档中关于DateTime对象和DateTimeZone的内容,并尝试了很多方法,但似乎无法得到正确的本地时间。我目前的代码如下:
$datetime = '2017-05-29T10:30:00Z'; // I'm getting this value from an API call
$timezone = 'Europe/Prague'; // I'm getting this value from an API call
$date = new \DateTime( $datetime, new \DateTimeZone( $timezone ) );
var_dump( $date->format('H:i') );
// I would expect it to be 12:30 since 29th May for this timezone would be GMT+2, but I get 10:30
显然,我对DateTime和DateTimeZone类的某些地方存在疑惑,但我无法理解。有没有人能够帮助我?
date_default_timezone_set
可以用于在该时区中打印当前时间 (date('H:i')
),但$date->format('H:i')
的结果不会因此而修改。 - jorditarridadate_default_timezone_set("Europe/Prague"); $datetime = date('Y-m-d').'T'.date('H:i:s').'Z'; $timezone = 'Europe/Prague'; $date = new \DateTime( $datetime, new \DateTimeZone( $timezone ) ); var_dump( $date->format('H:i') );
- Muhammad Athar$datetime = '2017-05-29T10:30:00Z';
。由于变量中的时间是10:30,因此你的代码正在打印10:30。 - Muhammad Athar