俄亥俄州哥伦布市的 php DateTime 时区

发布于 2024-09-25 03:04:11 字数 728 浏览 9 评论 0 原文

俄亥俄州哥伦布市位于 EDT 时区,但 PHP::DateTime 使用这些 php 时区 我似乎找不到一个给我正确时间的。美国/纽约晚了一个小时,因为在美国东部夏令时间结束之前我们是+5而不是4。

我很困惑。例如,现在大概是上午 11:26,我是上午 10:26 回来的。服务器设置了正确的时间,我尝试使用 date_default_timezone_set("EDT") 但这是不允许的。

$fromZone = 'Europe/London'; 
$toZone = 'American/New_York';
function adjustTime($time, $fromZone, $toZone){
 $date = new DateTime($time, new DateTimeZone($fromZone)); 
 $date->setTimezone(new DateTimeZone($toZone));
 return $date->format('Y-m-d H:i:s'); 
}

例如:我从肥皂网络服务“2010-09-23 15:25:56”收到,这是欧洲/伦敦时间,它应该转换为“2010-09-23 11:25:56”,这是 EDT 时间。我试图使用上面的函数,这返回“2010-09-23 10:25:56”

Columbus, Ohio is in EDT timezone but PHP::DateTime uses these php timezones and I cannot seem to find one that gives me correct time. America/New_York is off by an hour because until EDT ends we are +5 not 4.

I am confused. For example right now its 11:26AM roughly speaking and I get back 10:26AM. The server has the right time set on it, I tried using date_default_timezone_set("EDT") but this is not allowed.

$fromZone = 'Europe/London'; 
$toZone = 'American/New_York';
function adjustTime($time, $fromZone, $toZone){
 $date = new DateTime($time, new DateTimeZone($fromZone)); 
 $date->setTimezone(new DateTimeZone($toZone));
 return $date->format('Y-m-d H:i:s'); 
}

For example: I receive from a soap web service "2010-09-23 15:25:56" which is Europe/London time and it should be transformed to "2010-09-23 11:25:56" which is EDT time. I was trying to use the function above and this is returning back "2010-09-23 10:25:56"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九八野马 2024-10-02 03:04:11

鉴于在撰写本文时,“America/New_York”应该为您提供正确的值 纽约也是 11.36。请注意,这是 UTC-4(当前为 UTC 15:35)。在标准时间中,您是 UTC-5。

EDT 并不是真正的“完整”时区 - 它只是其中的白天部分。像“America/New_York”这样的时区名称提供了完整时区实现。

现在至于为什么你的服务器给出了错误的值,那是另一回事......如果你询问 UTC 时间,它会给你什么?

编辑:正如评论中提到的,修复方法是从 UTC 转换,而不是从欧洲/伦敦转换:(

$date = new DateTime($time, new DateTimeZone('UTC'))

可能有更好的方法;我不是 PHP 开发人员。)

"America/New_York" should give you the right value, given that at the time of this writing it's 11.36 in New York too. Note that that's UTC-4 (it's currently 15:35 UTC). In standard time, you're UTC-5.

EDT isn't really a "full" time zone - it's just the daylight part of it. A time zone name like "America/New_York" gives the complete time zone implementation.

Now as to why your server is giving the wrong value, that's a different matter... if you ask for the UTC time, what does it give you?

EDIT: As mentioned in the comments, the fix is to convert from UTC, not from Europe/London:

$date = new DateTime($time, new DateTimeZone('UTC'))

(There may be a better way of doing it; I'm not a PHP developer.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文