php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43003 Invalid timezone reported for DateTime objects constructed using a timestamp
Submitted: 2007-10-17 12:30 UTC Modified: 2008-01-17 19:59 UTC
Votes:4
Avg. Score:4.2 ± 0.8
Reproduced:4 of 4 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (25.0%)
From: post at jefago dot de Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.2.4 OS: Debian GNU/Linux
Private report: No CVE-ID: None
 [2007-10-17 12:30 UTC] post at jefago dot de
Description:
------------
A DateTime object that is created using a timestamp (with the "@$iTimestamp" notation) but without a explicitly given time zone (in the constructor) invalidly reports its time zone as the default time zone, although its time zone really is UTC. Converting it to the default time zone corrects this behaviour.

I know that this bug is somewhat similiar to http://bugs.php.net/bug.php?id=41912, that was marked as "bogus", but I think the demonstration code makes it clearer that in this case it is really buggy behaviour.

In fact, the UNIX timestamp is quite clearly defined as "the number of seconds since 1970-01-01 00:00:00 UTC" and thus it should be time zone independant.

Reproduce code:
---------------
$oDateTest = new DateTime("@0", new DateTimeZone(date_default_timezone_get()));
echo "timestamp 0, constructed with default time zone (".$oDateTest->getTimezone()->getName()."): " . $oDateTest->format("Y-m-d H:i:s")."<br />";
$oDateTest->setTimezone(new DateTimeZone("UTC"));
echo "timestamp 0, constructed with default time zone, set to ".$oDateTest->getTimezone()->getName()." afterwards: " . $oDateTest->format("Y-m-d H:i:s")."<br />";
$oDateTest->setTimezone(new DateTimeZone(date_default_timezone_get()));
echo "timestamp 0, constructed with default time zone, then set to UTC and then to default time zone (".$oDateTest->getTimezone()->getName().") again: " . $oDateTest->format("Y-m-d H:i:s")."<br />";

$oDateTest = new DateTime("@0");
echo "timestamp 0, constructed with default time zone (".$oDateTest->getTimezone()->getName()."): " . $oDateTest->format("Y-m-d H:i:s")."<br />";
$oDateTest->setTimezone( new DateTimeZone(date_default_timezone_get()));
echo "timestamp 0, constructed with default time zone, explicitly set to default time zone (".$oDateTest->getTimezone()->getName().") afterwards: " . $oDateTest->format("Y-m-d H:i:s")."<br />";


Expected result:
----------------
timestamp 0, constructed with default time zone (Europe/Berlin): 1970-01-01 01:00:00
timestamp 0, constructed with default time zone, set to UTC afterwards: 1970-01-01 00:00:00
timestamp 0, constructed with default time zone, then set to UTC and then to default time zone (Europe/Berlin) again: 1970-01-01 01:00:00
timestamp 0, constructed with default time zone (Europe/Berlin): 1970-01-01 01:00:00
timestamp 0, constructed with default time zone, explicitly set to default time zone (Europe/Berlin) afterwards: 1970-01-01 01:00:00

Actual result:
--------------
timestamp 0, constructed with default time zone (Europe/Berlin): 1970-01-01 00:00:00 (Either the time zone or the reported time is WRONG)
timestamp 0, constructed with default time zone, set to UTC afterwards: 1970-01-01 00:00:00
timestamp 0, constructed with default time zone, then set to UTC and then to default time zone (Europe/Berlin) again: 1970-01-01 01:00:00
timestamp 0, constructed with default time zone (Europe/Berlin): 1970-01-01 00:00:00 (Either the time zone or the reported time is WRONG)
timestamp 0, constructed with default time zone, explicitly set to default time zone (Europe/Berlin) afterwards: 1970-01-01 01:00:00

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-17 19:59 UTC] derick@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2011-02-07 09:11 UTC] jlammertink at gmail dot com
This bug still exists in the latest PHP version (5.3.x). Only way to get this 
right atm is to do it like this:

$dt = new DateTime(time(), new DateTimeZone('UTC'));
$dt->setTimeZone(new DateTimeZone('Europe/Amsterdam'));
 [2014-05-15 06:37 UTC] gxd305 at gmail dot com
this bug still exist in PHP 5.5.9
 [2018-08-23 10:44 UTC] favogo at zippiex dot com
this bug still exist in PHP 5.6.31
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC