views:

45

answers:

1

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"

+1  A: 

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

Jon Skeet
This does not work though. Using America/New_York gives me 10:26AM.
Chris
@Chris: But given that it *should* work, it means you should look elsewhere for the problem... such as checking that the server's idea of UTC is correct. It would also help if you'd post the code you're using to fetch the time.
Jon Skeet
What I am doing is retrieving strings containing time/date from a web service via soap which is based on Europe/London time. I wanted to take this for example "2010-09-23 15:25:56" which should become "2010-09-23 11:25:56". I will update with my code which performs transformation.
Chris
@Chris London time or UTC? There's a big difference... it would be odd IMO for a web service to use Europe/London instead of UTC.
Jon Skeet
Potentially I am wrong and it is UTC, how would I adjust the function above potentially to use this instead? I assumed london, as the company is based in London.
Chris
@Chris: In particular, "2010-09-23 15:25:56" was definitely the UTC time when it was 11:25:56 in Ohio.
Jon Skeet
@Chris, change the `$fromZone` to `UTC`.
salathe
@Chris: I've edited my answer to include salathe's comment, effectively.
Jon Skeet
@Jon Thanks so much, I feel very stupid for not realizing this. Just assumed they were ignorant and wanted time in relation to their HQ.
Chris
@Chris: No problem. UTC is a far more sensible time zone to use though, certainly :)
Jon Skeet