setTimezone can not set timezones without ID
| Request #45543 | DateTime::setTimezone can not set timezones without ID | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Submitted: | 2008-07-17 13:07 UTC | Modified: | 2014-07-30 10:52 UTC |
|
||||||||||
| From: | tj at systisoft dot com | Assigned: | derick (profile) | |||||||||||
| Status: | Closed | Package: | Date/time related | |||||||||||
| PHP Version: | 5.3CVS-2008-07-17 (CVS) | OS: | All | |||||||||||
| Private report: | No | CVE-ID: | None | |||||||||||
[2008-07-17 13:07 UTC] tj at systisoft dot com
Description:
------------
if you set a time zone for a DateTime via DateTime::setTimeZone() and that time zone has no ID you get an error message and the time zone will not be set.
This can lead to Problems if you have two DateTime objects from a surce you do not control and want to format the secont DateTime object in the same time zone as the first.
Reproduce code:
---------------
$d1 = new DateTime('2008-01-01 12:00:00 +02:00');
$d2 = new DateTime('2008-01-01 12:00:00 UTC');
echo $d1->format(DATE_ISO8601), PHP_EOL;
echo $d2->format(DATE_ISO8601), PHP_EOL;
$d2->setTimeZone($d1->getTimeZone());
echo $d1->format(DATE_ISO8601), PHP_EOL;
echo $d2->format(DATE_ISO8601), PHP_EOL;
Expected result:
----------------
2008-01-01T12:00:00+0200
2008-01-01T12:00:00+0000
2008-01-01T12:00:00+0200
2008-01-01T14:00:00+0200
Actual result:
--------------
2008-01-01T12:00:00+0200
2008-01-01T12:00:00+0000
PHP Warning: DateTime::setTimezone(): Can only do this for zones with ID for now in /Users/tobias/test.php on line 5
Warning: DateTime::setTimezone(): Can only do this for zones with ID for now in /Users/tobias/test.php on line 5
2008-01-01T12:00:00+0200
2008-01-01T12:00:00+0000
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2008-07-17 14:34 UTC] tj at systisoft dot com
Description: ------------ If you set a time zone for a DateTime via DateTime::setTimeZone() and that time zone has no ID you get an error message and the time zone will not be set. This can lead to problems if you have two DateTime objects from a source you do not control and want to format the second DateTime object in the same time zone as the first. Reproduce code: --------------- $d1 = new DateTime('2008-01-01 12:00:00 +02:00'); $d2 = new DateTime('2008-01-01 12:00:00 UTC'); echo $d1->format(DATE_ISO8601), PHP_EOL; echo $d2->format(DATE_ISO8601), PHP_EOL; $d2->setTimeZone($d1->getTimeZone()); echo $d1->format(DATE_ISO8601), PHP_EOL; echo $d2->format(DATE_ISO8601), PHP_EOL; Expected result: ---------------- 2008-01-01T12:00:00+0200 2008-01-01T12:00:00+0000 2008-01-01T12:00:00+0200 2008-01-01T14:00:00+0200 Actual result: -------------- 2008-01-01T12:00:00+0200 2008-01-01T12:00:00+0000 PHP Warning: DateTime::setTimezone(): Can only do this for zones with ID for now in /Users/tobias/test.php on line 5 Warning: DateTime::setTimezone(): Can only do this for zones with ID for now in /Users/tobias/test.php on line 5 2008-01-01T12:00:00+0200 2008-01-01T12:00:00+0000[2009-06-29 16:25 UTC] scott at crisscott dot com
I was able to reproduce this with PHP 5.2.9 from the commandline. php -v PHP 5.2.9 (cli) (built: May 1 2009 13:47:24) $> php -r '$d = new DateTime("2009-01-01 00:00:00+0400"); $d->setTimezone($d->getTimezone());'[2010-12-20 14:26 UTC] jani@php.net
-Package: Feature/Change Request +Package: Date/time related
[2011-01-08 19:56 UTC] wrzasq at gmail dot com
[2013-01-02 20:11 UTC] info at strictcoding dot co dot uk
[2013-02-26 19:49 UTC] rrollins at caltech dot edu
[2014-01-26 13:03 UTC] derick@php.net
-Status: Assigned +Status: Closed
[2014-07-30 10:48 UTC] phil at patient-radio dot de
[2014-07-30 10:48 UTC] phil at patient-radio dot de