php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42910 Constructing DateTime with TimeZone Indicator invalidates DateTimeZone
Submitted: 2007-10-09 23:33 UTC Modified: 2008-01-17 18:50 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: php at michaelho dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.2.4 OS: Mac OS X 10.4.10
Private report: No CVE-ID: None
 [2007-10-09 23:33 UTC] php at michaelho dot com
Description:
------------
When creating a new DateTime with an timezone indicator (e.g. "-0800"), it seems to ignore any DateTimeZone rules as specified either by the default_timezone setting OR by an explicit DateTimeZone parameter.

Notice that in the example below, the engine no longer applies DateTimeZone rules for $bar, even though the engine still lists "America/Los_Angeles" as $bar's DateTimeZone value.

My guess is that a timezone indicator is passed into the __construct() method, it will be very difficult for the engine to deduce the correct DateTimeZone rules to use for that DateTime object, and thus, no rules should be applied after this point.

If this is the case, then at the very least $bar->getTimezone()->getName() should return something like 'Undefined' or 'Custom/-0800' or something like that, to correctly indicate it is no longer using 'America/Los_Angeles' timezone rules.

Reproduce code:
---------------
	date_default_timezone_set('America/Los_Angeles');
	$foo = new DateTime('2007-03-11');
	$bar = new DateTime('2007-03-11T00:00:00-0800');

	print $foo->format(DateTime::ISO8601) . ' - ' . $foo->getTimezone()->getName() . ' - ' . $foo->format('U') . "\r\n";
	print $bar->format(DateTime::ISO8601) . ' - ' . $foo->getTimezone()->getName() . ' - ' . $bar->format('U') . "\r\n";

	$foo->setDate(2007, 03, 12);
	$bar->setDate(2007, 03, 12);

	print $foo->format(DateTime::ISO8601) . ' - ' . $foo->getTimezone()->getName() . ' - ' . $foo->format('U') . "\r\n";
	print $bar->format(DateTime::ISO8601) . ' - ' . $foo->getTimezone()->getName() . ' - ' . $bar->format('U') . "\r\n";


Expected result:
----------------
2007-03-11T00:00:00-0800 - America/Los_Angeles - 1173600000
2007-03-11T00:00:00-0800 - America/Los_Angeles - 1173600000
2007-03-12T00:00:00-0700 - America/Los_Angeles - 1173682800
2007-03-12T00:00:00-0700 - America/Los_Angeles - 1173682800

Actual result:
--------------
2007-03-11T00:00:00-0800 - America/Los_Angeles - 1173600000
2007-03-11T00:00:00-0800 - America/Los_Angeles - 1173600000
2007-03-12T00:00:00-0700 - America/Los_Angeles - 1173682800
2007-03-12T00:00:00-0800 - America/Los_Angeles - 1173686400

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-09 23:35 UTC] php at michaelho dot com
Typo... the first sentence in the third paragraph should read:

"My guess is that **IF** a timezone indicator is passed into the __construct() method..."
 [2007-10-09 23:42 UTC] derick@php.net
This is expected behavior, but I'll check your claim about getName().
 [2007-11-04 21:56 UTC] alex at kiesel dot name
I also believe that there should be a possibility to distinguish between a timezone "constructed" by an GMT offset and one constructed by an timezone identifier.

Here is a shorter reproducting script which shows the inconsistency:
<?php
  putenv('TZ=');
  date_default_timezone_set('Australia/Sydney');
  
  $date= date_create('2007-11-04 12:00:00+0200');
  var_dump(date_format($date, 'O e'));
?>

This prints:
$ php date.php 
string(22) "+0200 Australia/Sydney"
 [2008-01-17 18:50 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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 04:01:32 2024 UTC