Bug #68549 [Com]: Timezones and offsets are not properly used when working with dates

From: Date: Wed, 30 Dec 2015 22:07:42 +0000
Subject: Bug #68549 [Com]: Timezones and offsets are not properly used when working with dates
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-198322@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68549&edit=1

 ID:                 68549
 Comment by:         mj1856 at hotmail dot com
 Reported by:        mfaust at usinternet dot com
 Summary:            Timezones and offsets are not properly used when
                     working with dates
 Status:             Open
 Type:               Bug
 Package:            Date/time related
 Operating System:   Linux
 PHP Version:        5.6.3
 Block user comment: N
 Private report:     N

 New Comment:

As seen here:  http://stackoverflow.com/q/34533604/634824

$date = new \DateTime("now", new \DateTimeZone("UTC"));
$date->setTimestamp(1540686600);
echo $date->getTimestamp() . "\n";  //1540686600
echo $date->format('c') . "\n";     //2018-10-28T00:30:00+00:00

This is wrong, as the timestamp corresponds to 1:30:00+01:00

Additionally,
$date->setTimezone(new \DateTimeZone("Europe/London"));
echo $date->getTimestamp() . "\n";  //1540690200

This is really bad, as changing the timezone should NEVER change the timestamp.


Previous Comments:
------------------------------------------------------------------------
[2015-03-06 16:06:30] bestcoder at ymail dot com

This is another test case showing that daylight saving time is poorly handled.

<?php
echo "Expected DateTime in ISO8601: 2014-10-26T02:00:00+0200
Expected DateTime in RFC850: Sunday, 26-Oct-14 02:00:00 GMT+0200
Expected Timestamp: 1414281600\n";
echo "--------------------------------------------\n";

$date = new \DateTime("2014-10-26T02:00:00+02:00");
$date->setTimeZone(new \DateTimeZone('Europe/Paris'));

echo "Actual DateTime in ISO8601: ", $date->format(\DateTime::ISO8601) ,"\n";
echo "Actual DateTime in RFC850: ", $date->format(\DateTime::RFC850) ,"\n";
echo "Actual timestamp: ", $date->getTimestamp() ,"\n";


echo "--------------------------------------------\n";
echo "--------------------------------------------\n";



echo "Expected DateTime in ISO8601: 2014-10-26T03:00:00+0200
Expected DateTime in RFC850: Sunday, 26-Oct-14 03:00:00 GMT+0200
Expected timestamp: 1414285200\n";
echo "--------------------------------------------\n";

$date = new \DateTime("2014-10-26T03:00:00+02:00");
$date->setTimeZone(new \DateTimeZone('Europe/Paris'));

echo "Actual DateTime in ISO8601: ", $date->format(\DateTime::ISO8601) ,"\n";
echo "Actual DateTime in RFC850: ", $date->format(\DateTime::RFC850) ,"\n";
echo "Actual timestamp: ", $date->getTimestamp() ,"\n";
?>

This scripts run on PHP 5.5.9 output :

Expected DateTime in ISO8601: 2014-10-26T02:00:00+0200
Expected DateTime in RFC850: Sunday, 26-Oct-14 02:00:00 GMT+0200
Expected Timestamp: 1414281600
--------------------------------------------
Actual DateTime in ISO8601: 2014-10-26T02:00:00+0200
Actual DateTime in RFC850: Sunday, 26-Oct-14 02:00:00 CEST
Actual timestamp: 1414285200
--------------------------------------------
--------------------------------------------
Expected DateTime in ISO8601: 2014-10-26T03:00:00+0200
Expected DateTime in RFC850: Sunday, 26-Oct-14 03:00:00 GMT+0200
Expected timestamp: 1414285200
--------------------------------------------
Actual DateTime in ISO8601: 2014-10-26T02:00:00+0100
Actual DateTime in RFC850: Sunday, 26-Oct-14 02:00:00 CET
Actual timestamp: 1414285200


So according to PHP 'Sunday, 26-Oct-14 02:00:00 CEST' and 'Sunday, 26-Oct-14 02:00:00
CET' refer to same timestamp '1414285200'  which is wrong. For 'Sunday,
26-Oct-14 02:00:00 CEST', it should show 1414281600.

------------------------------------------------------------------------
[2014-12-05 05:33:55] yohgaki@php.net

http://3v4l.org/HGGjd

HHVM seems working correctly.

------------------------------------------------------------------------
[2014-12-05 05:22:55] mfaust at usinternet dot com

Description:
------------
Unix timestamps are not respected during daylight savings transitions. When specifying a timestamp
during a repeated hour, DateTime silently(incorrectly) modifies the timestamp to the previous hour.

In regards to the proposed https://wiki.php.net/rfc/datetime_and_daylight_saving_time
change, there are some current behaviors in regards to the "fall back" repeated hour that
sorely need to be addressed. For those that understand and need proper and accurate date/time
handling it is crucial that these inconsistent behaviors are eliminated.


Test script:
---------------
//A timestamp indicates a fixed point in time, irregardless of timezone and daylight savings, so PHP
must not change it when setting it:
$x = new DateTime();
$x->setTimezone(new DateTimezone('America/Chicago'));
$x->setTimestamp(1446357600);
echo $x->format('c') . "\n";

$x->setTimestamp(1446361200);
echo $x->format('c') . "\n"; //This should have a different offset since
it's outside DST
echo $x->getTimestamp();

//Simpler example
$x = new DateTime();
$x->setTimezone(new DateTimezone('America/Chicago'));
$x->setTimestamp(1446361200);
echo $x->getTimestamp(); //Returns a different timestamp. No apparent way to specify a timestamp
during the repeated hour.



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=68549&edit=1


Thread (12 messages)

« previous php.bugs (#198322) next »