php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72338 DateTimeZone with "+HH:MM" string incorrectly modifies DateTime
Submitted: 2016-06-05 23:30 UTC Modified: 2019-07-22 10:09 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:0 (0.0%)
From: hoylen at hoylen dot com Assigned:
Status: Duplicate Package: Date/time related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-10 21:02 UTC] 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
 [2017-04-01 19:46 UTC] tpunt@php.net
-Package: date_time +Package: Date/time related
 [2017-04-04 12:16 UTC] igor_deyawka at mail dot ru
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.
 [2019-07-22 10:09 UTC] requinix@php.net
-Status: Open +Status: Duplicate
 [2019-07-22 10:09 UTC] requinix@php.net
Fixed since PHP 7.0.17/7.1.3. https://3v4l.org/QMpLM

Looks the same as bug #73489.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 22:01:31 2024 UTC