|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-06-05 23:30 UTC] hoylen at hoylen dot com
Description: ------------ --- From manual page: http://www.php.net/datetimezone.construct --- A DateTimeZone can be created with a string formatted like an offset time (e.g. "+10:00"). It does not reject the value. But when that DateTimeZone is used with the setTimezone method on a DateTime object, it changes the underlying time, instead of simply allowing the DateTime to be simply displayed in that timezone. This bug is most easily seen when setTimezone is invoked more than once. The first invocation works, but the subsequent ones (even if used with a correct DateTimeZone) produces the wrong results. Test script: --------------- <?php $dt1 = new DateTime("2016-01-01 20:00:00+00:00"); $dt2 = new DateTime("2016-01-01 20:00:00+00:00"); $tz_ok= new DateTimeZone('Europe/Paris'); $tz_bad = new DateTimeZone('+01:00'); for ($i = 0; $i < 5; $i++) { $dt1->setTimezone($tz_ok); $dt2->setTimezone($tz_bad); print($dt1->format(DATE_RFC822) . " -- " .$dt2->format(DATE_RFC822) . "\n")\ ; } $dt2->setTimezone($tz_ok); print($dt2->format(DATE_RFC822) . "\n"); Expected result: ---------------- Fri, 01 Jan 16 21:00:00 +0100 -- Fri, 01 Jan 16 21:00:00 +0100 Fri, 01 Jan 16 21:00:00 +0100 -- Fri, 01 Jan 16 21:00:00 +0100 Fri, 01 Jan 16 21:00:00 +0100 -- Fri, 01 Jan 16 21:00:00 +0100 Fri, 01 Jan 16 21:00:00 +0100 -- Fri, 01 Jan 16 21:00:00 +0100 Fri, 01 Jan 16 21:00:00 +0100 -- Fri, 01 Jan 16 21:00:00 +0100 Fri, 01 Jan 16 21:00:00 +0100 Actual result: -------------- Fri, 01 Jan 16 21:00:00 +0100 -- Fri, 01 Jan 16 22:00:00 +0100 Fri, 01 Jan 16 21:00:00 +0100 -- Fri, 01 Jan 16 23:00:00 +0100 Fri, 01 Jan 16 21:00:00 +0100 -- Sat, 02 Jan 16 00:00:00 +0100 Fri, 01 Jan 16 21:00:00 +0100 -- Sat, 02 Jan 16 01:00:00 +0100 Fri, 01 Jan 16 21:00:00 +0100 -- Sat, 02 Jan 16 02:00:00 +0100 Sat, 02 Jan 16 02:00:00 +0100 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 08 12:00:02 2025 UTC |
I can add an additional information. If you use getTimestamp method between setTimezone methods - this problem goes away. Test script: --------------- <?php $date = new DateTime('@0'); $date->setTimezone(timezone_open('+01:00')); echo $date->format('Y-m-d H:i:s P') . "\n"; $date->setTimezone(timezone_open('+01:00')); echo $date->format('Y-m-d H:i:s P') . "\n"; $date = new DateTime(); $date->setTimezone(timezone_open('+01:00')); echo $date->format('Y-m-d H:i:s P') . "\n"; $date->getTimestamp(); $date->setTimezone(timezone_open('+01:00')); echo $date->format('Y-m-d H:i:s P') . "\n"; Expected result: ---------------- 1970-01-01 01:00:00 +01:00 1970-01-01 01:00:00 +01:00 1970-01-01 01:00:00 +01:00 1970-01-01 01:00:00 +01:00 Actual result: -------------- 1970-01-01 01:00:00 +01:00 1970-01-01 02:00:00 +01:00 1970-01-01 01:00:00 +01:00 1970-01-01 01:00:00 +01:00