note 74599 added to function.date-create

From: Date: Thu, 19 Apr 2007 12:47:22 +0000
Subject: note 74599 added to function.date-create
Groups: php.notes 
Request: Send a blank email to php-notes+get-125568@lists.php.net to get a copy of this message
"String in a format accepted by strtotime()" is not 100% truth - you cannot pass timezone info in the string used as DateTime constructor, while you can do it with strtotime(). It may be a problem if you would like to create a date from GMT time and then display it in your local timezone, for example: <?php $timeZone = 'Europe/Warsaw'; // +2 hours date_default_timezone_set($timeZone); $dateSrc = '2007-04-19 12:50 GMT'; $dateTime = new DateTime($dateSrc); echo 'date(): '.date('H:i:s', strtotime($dateSrc)); // correct! date(): 14:50:00 echo 'DateTime::format(): '.$dateTime->format('H:i:s'); // INCORRECT! DateTime::format(): 12:50:00 ?> So if you want to convert date between different timezones, you have to create two DateTimeZone objects - one for the input and one for output, like this: <?php $timeZone = 'Europe/Warsaw'; // +2 hours $dateSrc = '2007-04-19 12:50'; $dateTime = new DateTime($dateSrc, new DateTimeZone('GMT')); $dateTime->setTimeZone(new DateTimeZone($timeZone)); echo 'DateTime::format(): '.$dateTime->format('H:i:s'); // CORRECT! DateTime::format(): 14:50:00 ?> I'm not sure if this is a bug or desired behaviour. ---- Server IP: 83.64.128.68 Probable Submitter: 83.25.199.179 ---- Manual Page -- http://www.php.net/manual/en/function.date-create.php Edit -- https://master.php.net/note/edit/74599 Del: integrated -- https://master.php.net/note/delete/74599/integrated Del: useless -- https://master.php.net/note/delete/74599/useless Del: bad code -- https://master.php.net/note/delete/74599/bad+code Del: spam -- https://master.php.net/note/delete/74599/spam Del: non-english -- https://master.php.net/note/delete/74599/non-english Del: in docs -- https://master.php.net/note/delete/74599/in+docs Del: other reasons-- https://master.php.net/note/delete/74599 Reject -- https://master.php.net/note/reject/74599 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125568) next »