php 如何将UTC日期时间转换为另一个时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11883757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert UTC datetime to another timezone?
提问by Jo Smo
How can i convert a date like this: 2012-07-16 01:00:00 +00(it's in the UTC +00:00timezone) to UTC +04:00timezone? Ensuring that daylight saving will be handelled correctly?
我如何将这样的日期:(2012-07-16 01:00:00 +00它在UTC +00:00时区中)转换UTC +04:00为时区?确保正确处理夏令时?
回答by Florent
Use DateTimeand DateTimeZone.
$date = new DateTime('2012-07-16 01:00:00 +00');
$date->setTimezone(new DateTimeZone('Europe/Moscow')); // +04
echo $date->format('Y-m-d H:i:s'); // 2012-07-15 05:00:00
回答by Fluffeh
To help with the solution, you need to get the last part of the string (the offset part) and look it up against a simple lookup. you can use a regex or substr()(maybe) to get the offest part. Then, when you have a + or - value, use a maximum of 24 lookups against possible timezones which you can use with PHP's possible timezones- if the offset is the same, who cares what the actual country/location is?
为了帮助解决这个问题,您需要获取字符串的最后一部分(偏移部分)并通过简单的查找进行查找。您可以使用正则表达式或substr()(也许)来获取最重要的部分。然后,当您有 + 或 - 值时,最多对可能的时区使用 24 次查找,您可以将这些时区与 PHP 的可能时区一起使用- 如果偏移量相同,谁在乎实际的国家/地区是什么?
The use date_default_timezone_setto apply the right one.
使用date_default_timezone_set来应用正确的。
回答by Gaurav Singh
You can also use GMT time also and convert it to your requirement afterwards
您也可以使用 GMT 时间,然后将其转换为您的要求
<?php
echo gmdate("M d Y H:i:s", mktime(0, 0, 0, 1, 1, 1998));
?>
GMT refers Greenwich Mean Time which is common all over the world.
GMT 是指格林威治标准时间,在世界范围内通用。

