note 116027 added to datetime.createfromformat

From: Date: Fri, 31 Oct 2014 10:04:19 +0000
Subject: note 116027 added to datetime.createfromformat
Groups: php.notes php.notes 
Request: Send a blank email to php-notes+get-200755@lists.php.net to get a copy of this message
It can be confusing creating new DateTime from timestamp when your default timezone (date.timezone) is different from UTC and you are used to date()-function. date()-function automatically uses your current timezone setting but DateTime::createFromFormat (or DateTime constructor) does not (it ignores tz-parameter). You can get same results as date() by setting the timezone after object creation. <?php $ts = 1414706400; $date1 = date("Y-m-d H:i", $ts); $date2 = DateTime::createFromFormat("U", $ts)->setTimeZone(new DateTimeZone(date_default_timezone_get()))->format("Y-m-d H:i"); //$date1===$date2 ?> ---- Server IP: 72.52.91.14 Probable Submitter: 91.157.68.121 ---- Manual Page -- http://php.net/manual/en/datetime.createfromformat.php Edit -- https://master.php.net/note/edit/116027 Del: integrated -- https://master.php.net/note/delete/116027/integrated Del: useless -- https://master.php.net/note/delete/116027/useless Del: bad code -- https://master.php.net/note/delete/116027/bad+code Del: spam -- https://master.php.net/note/delete/116027/spam Del: non-english -- https://master.php.net/note/delete/116027/non-english Del: in docs -- https://master.php.net/note/delete/116027/in+docs Del: other reasons-- https://master.php.net/note/delete/116027 Reject -- https://master.php.net/note/reject/116027 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#200755) next »