Bug #72338 [Com]: DateTimeZone with "+HH:MM" string incorrectly modifies DateTime
[email protected] ("igor_deyawka at mail dot ru")
| Newsgroups | php.bugs |
|---|---|
| Message-ID | <[email protected]> |
Edit report at https://bugs.php.net/bug.php?id=72338&edit=1
ID: 72338
Comment by: igor_deyawka at mail dot ru
Reported by: hoylen at hoylen dot com
Summary: DateTimeZone with "+HH:MM" string incorrectly
modifies DateTime
Status: Open
Type: Bug
Package: Date/time related
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
I realised, that this bug doesn't appears in php 7.0, 7.1.
It surely appears in php 5.5, 5.6 on Linux. And i am not sure but as i remember it appears on 5.7 on Linux and Windows.
Previous Comments:
------------------------------------------------------------------------
[2016-10-10 21:02:47] igor_deyawka at mail dot ru
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
------------------------------------------------------------------------
[2016-06-05 23:30:32] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72338&edit=1