UTC日期/时间字符串转换为时区。

65

如何将一个日期/时间字符串(例如2011-01-01 15:00:00)从UTC转换为php支持的任何给定时区,例如美国/纽约或欧洲/圣马力诺。

6个回答

158

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');

这就是它。你做到了! - Mike Aron
1
它非常灵活,甚至可以接受 $text = '2019-02-15T17:10:46+05:30'; $dateTime = new DateTime($date); echo $dateTime->format("M d, Y"); - Sorter
@Sorter 是的,你可以返回这样格式的日期,但这个问题特别是关于转换时区的。由于你的日期时间字符串已经包含了TZ(原帖没有),你可以跳过我在new DateTime调用中设置$UTC的部分,并将$UTC添加到setTimezone调用中,从而将你的TZ从 +5:30 转换为UTC! - Kevin Peno
1
我已经为此奋斗了一天半!这就是我需要的! - Kelso

5

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年4月21日09:14:00


1

假设字符串中不包含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对象。


1
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);

0

通用的规范化函数,可将任何时区的任何时间戳格式化为其他格式。非常适用于在关系型数据库中存储来自不同时区用户的日期时间戳。对于数据库比较,请将时间戳存储为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"

-2

怎么样:

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

$date->format() 需要一个参数。请参考此链接 - Raptor
$date = new DateTime('2011-04-21 13:14', $timezone) 这不是缺少了UTC的秒数吗? - Quadrivium
除非我漏掉了什么,否则这段代码(即使修复为使用format作为方法)也不会按要求在时区之间进行转换:$date将位于UTC。 - Álvaro González

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接