note 104111 modified in datetime.construct by danbrown

From: Date: Tue, 24 May 2011 12:41:51 +0000
Subject: note 104111 modified in datetime.construct by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-179996@lists.php.net to get a copy of this message
A definite "gotcha" (while documented) that exists in the __construct is that it ignores your timezone if the $time is a timestamp. While this may not make sense, the object does provide you with methods to work around it. <?php // New Timezone Object $timezone = new DateTimeZone('America/New_York'); // New DateTime Object $date = new DateTime('@1306123200', $timezone); // You would expect the date to be 2011-05-23 00:00:00 // But it actually outputs 2011-05-23 04:00:00 echo $date->format('Y-m-d H:i:s'); // You can still set the timezone though like so... $date->setTimezone($timezone); // This will now output 2011-05-23 00:00:00 echo $date->format('Y-m-d H:i:s'); ?> --was-- A definite "gotcha" (while documented) that exists in the __construct is that it ignores your timezone if the $time is a timestamp. While this may not make sense, the object does provide you with methods to work around it. // New Timezone Object $timezone = new DateTimeZone('America/New_York'); // New DateTime Object $date = new DateTime('@1306123200', $timezone); // You would expect the date to be 2011-05-23 00:00:00 // But it actually outputs 2011-05-23 04:00:00 echo $date->format('Y-m-d H:i:s'); // You can still set the timezone though like so... $date->setTimezone($timezone); // This will now output 2011-05-23 00:00:00 echo $date->format('Y-m-d H:i:s'); http://php.net/manual/en/datetime.construct.php

« previous php.notes (#179996) next »