PHP将UTC中的字符串日期时间转换为GMT + 7格式

时间:2018-10-02 04:27:26

标签: php datetime timezone date-formatting

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

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

1 个答案:

答案 0 :(得分:5)

您可以尝试一下。这是php timezone

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