note 96016 deleted from datetime.construct by danielc

From: Date: Sat, 24 Apr 2010 20:08:25 +0000
Subject: note 96016 deleted from datetime.construct by danielc
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-168594@lists.php.net to get a copy of this message
Note Submitter: paul at smartposition dot nl ---- 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' ?>

« previous php.notes (#168594) next »