Tuesday, March 9, 2010

Parsing javascript date string to timestamp. the tz info is in the date!

Programmer Question

I have to parse a javascript date string to a timestamp. If the date string has the TZ info, why do I have to supply a TZ object in the DateTime constructor and again with setTimezone()? Is there an easier way to do this that is aware of the TZ info in the date?



$s = 'Thu Mar 11 2010 13:00:00 GMT-0500 (EST)';
$dt_obj = new DateTime($s, new DateTimeZone('America/New_York')); /* why? the TZ info is in the date string */

// again
$dt_obj->setTimezone(new DateTimeZone('UTC'));

echo 'timestamp ' , $dt_obj->getTimestamp(), '<br>';


Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails