【问题标题】:Wrong result when converting time to UTC将时间转换为 UTC 时结果错误
【发布时间】:2011-07-25 03:43:47
【问题描述】:

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

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 时区时。我得到错误的结果....... 请检查代码并帮助我修复错误。

【问题讨论】:

  • 你为什么要篡改你的问题?

标签: php timezone utc


【解决方案1】:

结果是“Current Date/Time is = 2011-03-29 13:23:00”和“America/New_York timezone”,看起来是正确的。

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

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

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

【讨论】:

    【解决方案2】:

    我猜这个问题(和代码)可能与 this one 有关。所以你的解决方案就在那里。顺便说一句,您是否发现将不同时区的时间转换为 UTC 有什么问题?然后,您不需要使用函数的最后一个参数(布尔值 true/false),因为 UTC 不遵守夏令时,所以基本上您将获得与函数结果相同的时间,无论函数中是否包含 DST。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-27
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 2018-07-27
      • 2021-09-05
      • 1970-01-01
      相关资源
      最近更新 更多