PHP将UTC中的字符串datetime转换为GMT+;7格式

PHP将UTC中的字符串datetime转换为GMT+;7格式,php,datetime,timezone,date-formatting,Php,Datetime,Timezone,Date Formatting,我有一个Y-m-dh:i:sUTC格式的日期时间字符串,我想将此字符串转换为Y-m-dh:i:sGMT+7格式,例如: $utcDateTime = '2018-10-02 04:08:17'; $gmtDateTime = $this->convertDateTime($utcDateTime); echo $gmtDateTime; // 2018-10-02 11:01:02 你可以试试这个。这是php 你可以试试这个。这是php function convertDateTime

我有一个
Y-m-dh:i:s
UTC格式的日期时间字符串,我想将此字符串转换为
Y-m-dh:i:s
GMT+7格式,例如:

$utcDateTime = '2018-10-02 04:08:17';
$gmtDateTime = $this->convertDateTime($utcDateTime);
echo $gmtDateTime; // 2018-10-02 11:01:02 

你可以试试这个。这是php


你可以试试这个。这是php

function convertDateTime($date, $format = 'Y-m-d H:i:s')
{
    $tz1 = 'UTC';
    $tz2 = 'Antarctica/Davis'; // UTC +7

    $d = new DateTime($date, new DateTimeZone($tz1));
    $d->setTimeZone(new DateTimeZone($tz2));

    return $d->format($format);
}

$utcDateTime = '2018-10-02 04:08:17';

echo convertDateTime($utcDateTime); // 2018-10-02 11:08:17