【问题标题】:Getting local time in PHP given a ISO 8601 datetime and timezone在给定 ISO 8601 日期时间和时区的情况下,在 PHP 中获取本地时间
【发布时间】:2017-02-04 17:57:34
【问题描述】:

我正在尝试获取一个本地时间,给定一个 ISO 8601 格式的祖鲁时间日期时间字符串和一个时区字符串。我阅读了有关 DateTime 对象和 DateTimeZone 的 PHP 文档,并且尝试了很多东西,但我似乎无法获得正确的本地时间。我现在拥有的:

$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 类的一些内容,但我无法正确理解。有人能帮我吗?

【问题讨论】:

    标签: php date datetime time iso8601


    【解决方案1】:

    所以我终于做对了,任何有兴趣的人:

    $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( 'UTC' ) ); // as the original datetime string is in Zulu time, I need to set the datetimezone as UTC when creating
    $date->setTimezone( new \DateTimeZone( $timezone ) ); // this is what actually sets which timezone is printed in the next line
    var_dump( $date->format('H:i') ); // yay! now it prints 12:30
    

    【讨论】:

      【解决方案2】:

      我认为混淆在于您所在的时区而不是日期时间字符串所在的时区。我在新奥尔良,所以我的时区是“美国/芝加哥”。我会这样设置:

      date_default_timezone_set('America/Chicago');
      

      然后,设置您的日期时间字符串来自的时区(我假设是欧洲/布拉格):

      $datetimePrague = '2017-05-29T10:30:00Z';
      $timezonePrague = 'Europe/Prague';
      

      现在,当您创建 DateTime 对象时,将日期时间和时间从哪里来的时区传递给它。

      $datePrague = new \DateTime( $datetimePrague, new \DateTimeZone( $timezonePrague ) );
      

      然后,获取 DateTime 对象的时间戳:

      $timestamp = $datePrague->getTimestamp();
      

      最后,将该时间戳连同您想要的格式一起传递给 date() 函数,无论您在哪个时区,您都将拥有相当于 DateTime 的值:

      date('H:i', $timestamp);
      

      这是完整的代码,假设我住在新奥尔良,我想知道布拉格时间 10:30 的新奥尔良当地时间:

      <?php
      
      date_default_timezone_set('America/Chicago');
      
      $datetimePrague = '2017-05-29T10:30:00Z';
      $timezonePrague = 'Europe/Prague';
      $datePrague = new \DateTime( $datetimePrague, new \DateTimeZone( $timezonePrague ) );
      $timestamp = $datePrague->getTimestamp();
      var_dump(date('H:i', $timestamp));
      

      【讨论】:

      • 感谢您非常详细的回答。我其实想得到当地时间:不是我自己的时间,而是根据$timezone中定义的时区的时间,因为原始的日期时间字符串是祖鲁时间(即UTC,所以不是当地时间->这是 ISO 8601 日期格式中的 Z)。因此,尽管您的回答非常清晰且有帮助,但它并没有帮助我解决我的特定问题。我终于找到了解决方法并写了一个答案。尽管如此,我还是赞成您的回答如此详细和乐于助人:)
      • 好吧,看来我没有足够的声望来支持我的投票,但我还是给了它。再次感谢。
      • 啊,我想我很困惑。我认为欧洲/布拉格是您的日期时间字符串的来源。很高兴你明白了!
      【解决方案3】:

      试试这个代码:

      date_default_timezone_set("Europe/Prague");
      echo date('H:i');
      

      它将以 24 小时格式打印日期和时间。

      【讨论】:

      • date_default_timezone_set 用于打印该时区的当前时间 (date('H:i')),但 $date-&gt;format('H:i') 的结果不会被此修改。
      • 这对我有用:date_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-&gt;format('H:i') );
      • 您正在尝试使用静态变量 $datetime = '2017-05-29T10:30:00Z';。var 中的时间是 10:30,因此您的代码正在打印 10:30
      • 我从 API 调用中得到这个,所以我不能改变它。我已经更新了问题以反映这一点。正如我在原始问题中所述,我正在尝试从给定的 ISO 8601 字符串 + 时区获取本地时间,因此您的解决方案对我不起作用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多