0
这是很容易给定的GMT日期转换为本地时间,如果你给从这个名单在PHP中的时区标识符转换GMT时间为当地时间:http://www.php.net/manual/en/timezones.php使用时区偏移,而不是时区标识符
例如,你可以这样做(其中$ fromTimeZone仅仅是“GMT”,$ toTimeZone是刚刚从列表(即“美国/芝加哥”)中的一个常量,而$日期时间是格林尼治标准时间为准):
public static function convertToTimezone($datetime, $fromTimeZone, $toTimeZone, $format = 'Y-m-d H:i')
{
// Construct a new DateTime object from the given time, set in the original timezone
$convertedDateTime = new DateTime($datetime, timezone_open($fromTimeZone));
// Convert the published date to the new timezone
$convertedDateTime->setTimezone(timezone_open($toTimeZone));
// Return the udpated date in the format given
return $convertedDateTime->format($format);
}
但是,如果只是给定时区偏移量,我有问题将相同的GMT日期转换为本地时间。例如,我给出的不是“美国/芝加哥”,而是给予-0500(这是该时区的等价抵消)。
我试过的东西,如以下(其中$日期时间是我的GMT日期和$ toTimeZone是偏移量(在这种情况下,-0500)):
date($format, strtotime($datetime . ' ' . $toTimeZone))
我知道所有的日期()各种功能都基于服务器的时区。我似乎无法让它忽略它,并使用明确给出的时区偏移量。
可能的[Javascript/PHP和时区]的副本(http://stackoverflow.com/questions/2319451/javascript-php-and-timezones) – joshholat 2012-04-20 16:36:07
您有数字偏移量。你为什么不加上或减去适当的小时数和分钟数? – Celada 2012-04-20 17:56:27