简体   繁体   中英

php DateTime timezone for Columbus Ohio

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"

"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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM