|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 05:00:01 2025 UTC |
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+0000I 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());'