note 96016 added to datetime.construct

From: Date: Wed, 03 Feb 2010 11:07:35 +0000
Subject: note 96016 added to datetime.construct
Groups: php.notes 
Request: Send a blank email to php-notes+get-165617@lists.php.net to get a copy of this message
At least with php 5.2.12, the behavior of the DateTime constructor in combination with timezones surprised me a bit. Apparently, the second argument (timezone) is completely ignored when the first argument (time) already contains timezone info; I expected it to transform 'time' to 'timezone' instead. So, with my system being on 'Europe/Amsterdam' (+01:00): <?php $date = new DateTime('2010-01-28T15:00:00' ); echo $date->format( 'c' ); // Outputs: '2010-01-28T15:00:00+01:00' ?> Works as expected, and so does: <?php $date = new DateTime('2010-01-28T15:00:00', new DateTimezone( 'America/Chicago' )); echo $date->format( 'c' ); // Outputs: '2010-01-28T15:00:00-06:00' ?> However: <?php $date = new DateTime('2010-01-28T15:00:00+02:00', new DateTimezone( 'UTC' ) ); echo $date->format( 'c' ); // Outputs '2010-01-28T15:00:00+02:00' ?> Which is the same output as when the second argument isn't present at all; I'd expect the date to be transformed to the given DateTimezone. To achieve this effect however, you'll need an additional 'setTimezone' call: <?php $date = new DateTime('2010-01-28T15:00:00+02:00' ); echo $date->format( 'c' ); // Outputs '2010-01-28T15:00:00+02:00' $date->setTimezone( new DateTimezone( 'UTC' ) ); echo $date->format( 'c' ); // Outputs '2010-01-28T13:00:00+00:00' ?> ---- Server IP: 69.147.83.197 Probable Submitter: 86.82.134.169 ---- Manual Page -- http://www.php.net/manual/en/datetime.construct.php Edit -- https://master.php.net/note/edit/96016 Del: integrated -- https://master.php.net/note/delete/96016/integrated Del: useless -- https://master.php.net/note/delete/96016/useless Del: bad code -- https://master.php.net/note/delete/96016/bad+code Del: spam -- https://master.php.net/note/delete/96016/spam Del: non-english -- https://master.php.net/note/delete/96016/non-english Del: in docs -- https://master.php.net/note/delete/96016/in+docs Del: other reasons-- https://master.php.net/note/delete/96016 Reject -- https://master.php.net/note/reject/96016 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#165617) next »