note 111869 deleted from datetime.construct by aharvey
| From: | aharvey@php.net | Date: | Mon, 08 Apr 2013 01:27:18 +0000 |
| Subject: | note 111869 deleted from datetime.construct by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-194387@lists.php.net to get a copy of this message | ||
Note Submitter: rbotzer at yahoo dot com
----
This method does not behave as expected when the time argument is an ISO-8601 string.
You would expect that this method creates a Unix timestamp, which is absolute (the number of seconds
since January 1 1970 UTC), then expresses it in the timezone described in the second argument.
However, it stays in the timezone described by the ISO-8601 string.
If you want to express the the DateTime object in a different timezone you must explicitly
setTimezone to the destination timezone.
For example, 12:49PDT in Central Daylight Time (+2 hours):
-------------------------------------------
Without setTimezone:
<?php
$destination_tz = new DateTimeZone('America/Chicago');
$dt = new DateTime("2013-04-05T12:49:02-07:00", $destination_tz);
var_dump($dt->format('H:ia'));
?>
-------------------------------------------
string(7) "12:49pm"
-------------------------------------------
With setTimezone:
<?php
$destination_tz = new DateTimeZone('America/Chicago');
$utc = new DateTimeZone('UTC');
$dt = new DateTime("2013-04-05T12:49:02-07:00", $utc);
$dt->setTimezone($destination_tz);
var_dump($dt->format('H:ia'));
?>
-------------------------------------------
string(7) "14:49pm"
-------------------------------------------
To see what's inside the DateTime before the setTimezone method is called:
<?php
$destination_tz = new DateTimeZone('America/Chicago');
$dt = new DateTime("2013-04-05T12:49:02-07:00", $destination_tz);
var_dump($dt);
?>
-------------------------------------------
object(DateTime)#3 (3) {
["date"]=>
string(19) "2013-04-05 12:49:02"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "-07:00"
}
And that, my friends, is just WRONG.