将时间转换为UTC时错误的结果

时间:2011-03-29 10:01:03

标签: php timezone utc

我写了一个脚本,将我的时区转换为gmt,反之亦然.......但是当我尝试检查DST跟随timzones的相同功能后,我得到了错误的结果。 这是我的功能............

function ConvertOneTimezoneToAnotherTimezone($time,$currentTimezone,$timezoneRequired)
{

 $current_zone = new DateTimeZone($currentTimezone);
// print_r($current_zone);
 //$gmt = new DateTimeZone('GMT');

 $date = new DateTime($time, $current_zone);
//var_dump($date);
 //$date->setTimezone($gmt);
 $date->setTimezone(new DateTimeZone($timezoneRequired));

 return  $date->format('Y-m-d H:i:s');

// Convert it back to Original timezone
 //$date->setTimezone($current_zone);
 //return  $date->format('Y-m-d H:i:s');
}

$time='2011-03-29 9:53:00';

echo  "Current Date/Time is = ".
      ConvertOneTimezoneToAnotherTimezone($time,'Asia/Kolkata','UTC');

此功能适用于“亚洲/加尔各答”,但是当我尝试使用America / New_York时区时。我得错了结果....... 请检查代码并帮我修复错误。

2 个答案:

答案 0 :(得分:2)

结果是“当前日期/时间是= 2011-03-29 13:23:00”,带有“America / New_York timezone”,似乎它是正确的。

你可能会觉得奇怪的是,纽约是GMT-5,应该是“2011-03-29 14:23:00”。

但是,夏令时(夏令时)于2011年3月13日开始,将于2011/11/6结束。 因此,在夏令时期间,它将与GMT-4相同。

使用夏令时检查纽约时间: http://www.timeanddate.com/worldclock/city.html?n=179

答案 1 :(得分:0)

我猜这个问题(和代码)可能与 this one 有关。所以你的解决方案在那里。顺便说一下,你有没有发现从不同的时区转换到UTC的时间有什么问题?你不需要使用函数的最后一个参数(布尔值true / false),因为UTC不会观察夏令时,所以基本上你会得到与函数结果相同的时间,函数中包含或不包含DST。

相关问题