使用格林威治标准时间 php 转换日期


Convert date with GMT php

>我得到这种格式的日期:2016-09-23T19:15:00+02:00 .

我如何使用 php 获取格式:2016-09-23 21:15 .

我试过了:

date('Y-m-d H:i', strtotime('2016-09-23T19:15:00+02:00');

但我得到

2016-09-23 19:15.

试一试:

function parseDate($date){
    $parts = explode("+", $date);
    if(isset($parts[1])) {
        $ph = (int)explode(":", $parts[1])[0];
        // take "02:00", extract "02" and convert to int. result: 2
        return date("Y-m-d H:i:s", strtotime($parts[0]." + $ph hour"));
    }
    return date("Y-m-d H:i:s", strtotime($date));
}
echo parseDate("2016-09-23T19:15:00+02:00");
// output 2016-09-23 21:15:00
echo parseDate("2016-09-23T19:15:00");
// output 2016-09-23 19:15:00

您可以在 3v4l.org 上检查它

注意:这样可以避免使用date_default_timezone_set()设置所有日期/时间函数使用的默认时区。如果您的应用程序已经设置了默认时区,则不建议重写它以进行简单的分析。