note 114366 added to datetime.format

From: Date: Thu, 13 Feb 2014 02:47:54 +0000
Subject: note 114366 added to datetime.format
Groups: php.notes 
Request: Send a blank email to php-notes+get-198485@lists.php.net to get a copy of this message
I believe this is a bug but its note-worthy if it is intended (I am using PHP 5.5.3). $ php --version PHP Warning: Module 'xdebug' already loaded in Unknown on line 0 PHP 5.5.3-1ubuntu2.1 (cli) (built: Dec 12 2013 04:24:35) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans with Zend OPcache v7.0.3-dev, Copyright (c) 1999-2013, by Zend Technologies What is happening: DateTime()->format() will modify the timezone. So do not expect the public date property to be returned (format mask applied) based on the current public timezone property. format will decide that when calling ->format() it will use the server timezone which eliminates all usefulness of ->setTimezone(). <?php $nowUtc = new \DateTime( 'now', new \DateTimeZone( 'UTC' ) ); echo '$nowUtc'.PHP_EOL; var_dump($nowUtc); $nowUtc = new \DateTime( 'now', new \DateTimeZone( 'UTC' ) ); echo '$nowUtc->format(\'Y-m-d h:i:s\')'.PHP_EOL; var_dump($nowUtc->format('Y-m-d h:i:s')); $nowUtc->setTimezone( new \DateTimeZone( 'Australia/Sydney' ) ); echo '$nowUtc->setTimezone( new \DateTimeZone( \'Australia/Sydney\' ) )'.PHP_EOL; var_dump($nowUtc); echo '$nowUtc->format(\'Y-m-d h:i:s\')'.PHP_EOL; var_dump($nowUtc->format('Y-m-d h:i:s'));exit; ?> outputs; $nowUtc object(DateTime)[2607] public 'date' => string '2014-02-13 02:42:48' (length=19) public 'timezone_type' => int 3 public 'timezone' => string 'UTC' (length=3) $nowUtc->format('Y-m-d h:i:s') string '2014-02-13 02:42:48' (length=19) $nowUtc->setTimezone( new \DateTimeZone( 'Australia/Sydney' ) ) object(DateTime)[2608] public 'date' => string '2014-02-13 13:42:48' (length=19) public 'timezone_type' => int 3 public 'timezone' => string 'Australia/Sydney' (length=16) $nowUtc->format('Y-m-d h:i:s') string '2014-02-13 01:42:48' (length=19) // expected 2014-02-13 13:42:48 based on Australia/Sydney - what is 2014-02-13 01:42:48 from anyway! ---- Server IP: 72.52.91.14 Probable Submitter: 72.52.91.14 ---- Manual Page -- http://php.net/manual/en/datetime.format.php Edit -- https://master.php.net/note/edit/114366 Del: integrated -- https://master.php.net/note/delete/114366/integrated Del: useless -- https://master.php.net/note/delete/114366/useless Del: bad code -- https://master.php.net/note/delete/114366/bad+code Del: spam -- https://master.php.net/note/delete/114366/spam Del: non-english -- https://master.php.net/note/delete/114366/non-english Del: in docs -- https://master.php.net/note/delete/114366/in+docs Del: other reasons-- https://master.php.net/note/delete/114366 Reject -- https://master.php.net/note/reject/114366 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198485) next »