note 103158 added to datetime.createfromformat
| From: | AurelienMarchand at osu1 dot php dot net | Date: | Tue, 29 Mar 2011 15:13:55 +0000 |
| Subject: | note 103158 added to datetime.createfromformat | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178304@lists.php.net to get a copy of this message | ||
Beware specifying a timezone in the format as it will take precedence over the DateTimeZone object.
<?php
$timezone = "UTC"; // or any other valid name for a timezone
$d= DateTime::createFromFormat("Y-m-d H:i:s T","2011-11-06 00:00:00 EDT",new
DateTimeZone($timezone));
echo $d->format("Y-m-d H:i:s T - U");
// returns "2011-11-06 00:00:00 EDT - 1320552000"
// specifying $timezone = "Pacific/Honolulu"; would return the same string
?>
This gets hairy when you are playing with transition from summer time to winter time! For instance,
in Toronto, the time change happens on 2011-11-06. One second after 01:59:59 (EDT), the time becomes
01:00:00 (EST), or 1320559200 in Unix timestamp.
However, notice the following:
<?php
$d = DateTime::createFromFormat("Y-m-d H:i:s","2011-11-06 01:00:00",new
DateTimeZone("EST"));
echo $d->format("Y-m-d H:i:s T U");
// returns "2011-11-06 01:00:00 EDT 1320555600" instead of "2011-11-06 01:00:00 EST
1320559200"
// so the correct way is to do:
$d = DateTime::createFromFormat("Y-m-d H:i:s T","2011-11-06 01:00:00 EST",new
DateTimeZone($timezone)); // set $timezone to any valid string for DateTimeZone, it doesn't
matter
echo $d->format("Y-m-d H:i:s T U");
// returns "2011-11-06 01:00:00 EST - 1320559200" as wanted
?>
----
Server IP: 66.207.199.35
Probable Submitter: 209.202.82.139
----
Manual Page -- http://www.php.net/manual/en/datetime.createfromformat.php
Edit -- https://master.php.net/note/edit/103158
Del: integrated -- https://master.php.net/note/delete/103158/integrated
Del: useless -- https://master.php.net/note/delete/103158/useless
Del: bad code -- https://master.php.net/note/delete/103158/bad+code
Del: spam -- https://master.php.net/note/delete/103158/spam
Del: non-english -- https://master.php.net/note/delete/103158/non-english
Del: in docs -- https://master.php.net/note/delete/103158/in+docs
Del: other reasons-- https://master.php.net/note/delete/103158
Reject -- https://master.php.net/note/reject/103158
Search -- https://master.php.net/manage/user-notes.php