【问题标题】:Convert datetime in GMT+# format to GMT将 GMT+# 格式的日期时间转换为 GMT
【发布时间】:2018-04-13 11:29:34
【问题描述】:

我有这样的输入格式,我需要将其转换为 GMT 格式:

$input = array(
           "gmt" => "+7",
           "datetime" => "2017-10-10 12:10:12"
         );

输入数据包含gmt数组索引,显示哪种gmt格式,日期时间索引显示“Y-m-d h:i:s”中需要从GMT+7转换为GMT的日期。

【问题讨论】:

标签: php datetime gmt


【解决方案1】:

试试这个:

$input = array(
  "gmt" => "+7",
  "datetime" => "2017-10-10 12:10:12"
);

$ny = new DateTimeZone("GMT+7");
$gmt = new DateTimeZone("GMT");
$date = new DateTime( $input["datetime"], $ny );
$date->setTimezone( $gmt );

echo $date->format('Y-m-d H:i:s');

【讨论】:

    【解决方案2】:

    Oneshot(不推荐):

    echo date('Y-m-d h:i:s', strtotime($input['datetime'])+$input['gmt']*3600);
    

    【讨论】:

      猜你喜欢
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 2020-05-10
      • 2016-11-12
      • 2012-07-04
      • 1970-01-01
      • 2019-03-17
      • 2019-01-27
      相关资源
      最近更新 更多