PHP将UTC转换为本地时间 [英] PHP converting UTC to Local Time
问题描述
在我的PostgreSQL中,我有一个名为"created"的列,其类型为带有时区的 timestamp
In my postgresql, the I have the following column named "created" that has the type timestamp with timezone
所以我按照我认为是UTC的格式插入了记录.
So I inserted the record according to the format as such which I believe is UTC.
2015-10-02 09:09:35 + 08
我正在使用php Carbon库,所以我做了以下事情:
I am using php Carbon library so i did the following:
$date = Carbon\Carbon::parse('2015-10-02 09:09:35+08');
echo $date->->toDatetimeString();
//gives result as 2015-10-02 09:09:35
如何使用该库回显正确的时区,包括以上述datetime格式添加+8?我正在使用的timzezone是亚洲/新加坡" .
How can I use the library to echo the correct timezone which includes the adding of the +8 in the above datetime format? The timzezone that I am using is "Asia/Singapore".
时间应打印为当地时间2015-10-02:17:09:35:
The time should be printed to local timing which is 2015-10-02: 17:09:35:
推荐答案
您可以使用本机php而不使用Carbon来完成此操作:
You can do this using native php without using Carbon:
$time = '2015-10-02 16:34:00+08';
$date = DateTime::createFromFormat('Y-m-d H:i:s+O', $time);
print $date->format('Y-m-d H:i:s') . PHP_EOL;
$date->setTimeZone(new DateTimeZone('Asia/Singapore'));
print $date->format('Y-m-d H:i:s') . PHP_EOL;
$date->setTimeZone(new DateTimeZone('Etc/UTC'));
print $date->format('Y-m-d H:i:s') . PHP_EOL;
这篇关于PHP将UTC转换为本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!