【问题标题】:UTC Date/Time String to TimezoneUTC 日期/时间字符串到时区
【发布时间】:2011-08-10 10:21:56
【问题描述】:

如何将 UTC 的日期/时间字符串(例如 2011-01-01 15:00:00)转换为 php 支持的任何给定时区,例如 America/New_York 或 Europe/San_Marino。

【问题讨论】:

    标签: php timezone utc


    【解决方案1】:

    PHP 的 DateTime 对象非常灵活。

    $UTC = new DateTimeZone("UTC");
    $newTZ = new DateTimeZone("America/New_York");
    $date = new DateTime( "2011-01-01 15:00:00", $UTC );
    $date->setTimezone( $newTZ );
    echo $date->format('Y-m-d H:i:s');
    

    【讨论】:

    • 就是这样。你做到了!
    • 它非常灵活,甚至需要$text = '2019-02-15T17:10:46+05:30'; $dateTime = new DateTime($date); echo $dateTime->format("M d, Y");
    • @Sorter 是的,您可以返回这样的日期并对其进行格式化,但这个问题专门关于转换时区。由于您的日期时间字符串已经包含 TZ(OP 没有),您可以跳过我在 new DateTime 调用中设置 $UTC 的部分,而是将 $UTC 添加到 setTimezone 调用中,将您的 TZ 转换为来自+5:30的UTC!
    • 我已经为此奋斗了一天半!这正是我所需要的!
    【解决方案2】:

    PHP 的 DateTime 对象非常灵活。

    由于用户要求提供多个时区选项,因此您可以将其设为通用。

    通用函数

    function convertDateFromTimezone($date,$timezone,$timezone_to,$format){
     $date = new DateTime($date,new DateTimeZone($timezone));
     $date->setTimezone( new DateTimeZone($timezone_to) );
     return $date->format($format);
    }
    

    用法:

    echo  convertDateFromTimezone('2011-04-21 13:14','UTC','America/New_York','Y-m-d H:i:s');
    

    输出:

    2011-04-21 09:14:00

    【讨论】:

      【解决方案3】:

      假设 UTC 不包含在字符串中:

      date_default_timezone_set('America/New_York');
      $datestring = '2011-01-01 15:00:00';  //Pulled in from somewhere
      $date = date('Y-m-d H:i:s T',strtotime($datestring . ' UTC'));
      echo $date;  //Should get '2011-01-01 10:00:00 EST' or something like that
      

      或者你可以使用 DateTime 对象。

      【讨论】:

        【解决方案4】:
        function _settimezone($time,$defaultzone,$newzone)
        {
        $date = new DateTime($time, new DateTimeZone($defaultzone));
        $date->setTimezone(new DateTimeZone($newzone));
        $result=$date->format('Y-m-d H:i:s');
        return $result;
        }
        
        $defaultzone="UTC";
        $newzone="America/New_York";
        $time="2011-01-01 15:00:00";
        $newtime=_settimezone($time,$defaultzone,$newzone);
        

        【讨论】:

          【解决方案5】:

          通用规范化函数,用于将任何时间戳从任何时区格式化为其他时区。 对于在关系数据库中存储来自不同时区的用户的日期时间戳非常有用。对于数据库比较,将时间戳存储为 UTC 并与 gmdate('Y-m-d H:i:s') 一起使用

          /**
           * Convert Datetime from any given olsonzone to other.
           * @return datetime in user specified format
           */
          
          function datetimeconv($datetime, $from, $to)
          {
              try {
                  if ($from['localeFormat'] != 'Y-m-d H:i:s') {
                      $datetime = DateTime::createFromFormat($from['localeFormat'], $datetime)->format('Y-m-d H:i:s');
                  }
                  $datetime = new DateTime($datetime, new DateTimeZone($from['olsonZone']));
                  $datetime->setTimeZone(new DateTimeZone($to['olsonZone']));
                  return $datetime->format($to['localeFormat']);
              } catch (\Exception $e) {
                  return null;
              }
          }
          

          用法:

          $from = ['localeFormat' => "d/m/Y H:i A", 'olsonZone' => 'Asia/Calcutta'];
          
          $to = ['localeFormat' => "Y-m-d H:i:s", 'olsonZone' => 'UTC'];
          
          datetimeconv("14/05/1986 10:45 PM", $from, $to); // returns "1986-05-14 17:15:00"
          

          【讨论】:

            【解决方案6】:

            怎么样:

            $timezone = new DateTimeZone('UTC');
            $date = new DateTime('2011-04-21 13:14', $timezone);
            echo $date->format;
            

            【讨论】:

            • $date->format() 需要 1 个参数。见this
            • $date = new DateTime('2011-04-21 13:14', $timezone) 这不是缺少UTC的秒数吗?
            猜你喜欢
            • 2020-07-18
            • 2014-11-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-10-22
            • 2014-02-22
            • 1970-01-01
            • 2011-06-13
            相关资源
            最近更新 更多