开发者

Wrong result when converting time to UTC

I am wrote a script which converts my timezone to gmt and vice versa perfectly.......but when i try to check the same function for DST following timzones i am getting wrong result. here is my function goes............

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->s开发者_如何学编程etTimezone($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');

this function works fine for 'Asia/Kolkata' but when i try with America/New_York timezone. i am getting wrong result....... please check the code and help me to fix the error.


The result is "Current Date/Time is = 2011-03-29 13:23:00" with "America/New_York timezone", and it seems that it's correct.

What you might think weird is that New York is GMT-5 and it should be "2011-03-29 14:23:00".

However, DST(Daylight Saving Time) started on 2011/3/13 and will ends on 2011/11/6. Thus, during DST period, it would be the same as GMT-4.

Check New York time with DST: http://www.timeanddate.com/worldclock/city.html?n=179


I guess this question (and code) could be related with this one. So your solution is there. Btw, have you found what's wrong about converting time to UTC from different timezones? You don't need to use last parameter of function (boolean true/false) then, because UTC doesn't observe daylight saving time so basically you will get same time as function's result, with or without DST included in function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜