note 111869 added to datetime.construct

From: Date: Fri, 05 Apr 2013 22:06:07 +0000
Subject: note 111869 added to datetime.construct
Groups: php.notes 
Request: Send a blank email to php-notes+get-194361@lists.php.net to get a copy of this message
This method does not behave as expected when the time argument is an ISO-8601 string. You would expect that this method creates a Unix timestamp, which is absolute (the number of seconds since January 1 1970 UTC), then expresses it in the timezone described in the second argument. However, it stays in the timezone described by the ISO-8601 string. If you want to express the the DateTime object in a different timezone you must explicitly setTimezone to the destination timezone. For example, 12:49PDT in Central Daylight Time (+2 hours): ------------------------------------------- Without setTimezone: <?php $destination_tz = new DateTimeZone('America/Chicago'); $dt = new DateTime("2013-04-05T12:49:02-07:00", $destination_tz); var_dump($dt->format('H:ia')); ?> ------------------------------------------- string(7) "12:49pm" ------------------------------------------- With setTimezone: <?php $destination_tz = new DateTimeZone('America/Chicago'); $utc = new DateTimeZone('UTC'); $dt = new DateTime("2013-04-05T12:49:02-07:00", $utc); $dt->setTimezone($destination_tz); var_dump($dt->format('H:ia')); ?> ------------------------------------------- string(7) "14:49pm" ------------------------------------------- To see what's inside the DateTime before the setTimezone method is called: <?php $destination_tz = new DateTimeZone('America/Chicago'); $dt = new DateTime("2013-04-05T12:49:02-07:00", $destination_tz); var_dump($dt); ?> ------------------------------------------- object(DateTime)#3 (3) { ["date"]=> string(19) "2013-04-05 12:49:02" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "-07:00" } And that, my friends, is just WRONG. ---- Server IP: 69.147.83.199 Probable Submitter: 108.224.137.221 ---- Manual Page -- http://php.net/manual/en/datetime.construct.php Edit -- https://master.php.net/note/edit/111869 Del: integrated -- https://master.php.net/note/delete/111869/integrated Del: useless -- https://master.php.net/note/delete/111869/useless Del: bad code -- https://master.php.net/note/delete/111869/bad+code Del: spam -- https://master.php.net/note/delete/111869/spam Del: non-english -- https://master.php.net/note/delete/111869/non-english Del: in docs -- https://master.php.net/note/delete/111869/in+docs Del: other reasons-- https://master.php.net/note/delete/111869 Reject -- https://master.php.net/note/reject/111869 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#194361) next »