php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #55642 DateTime doesn't use default timezone
Submitted: 2011-09-08 14:35 UTC Modified: 2012-02-25 16:30 UTC
From: alex dot whitman at durham dot ac dot uk Assigned: salathe (profile)
Status: Closed Package: Date/time related
PHP Version: 5.3.8 OS: CentOS 5
Private report: No CVE-ID: None
 [2011-09-08 14:35 UTC] alex dot whitman at durham dot ac dot uk
Description:
------------
DateTime doesn't appear to use the default timezone (set either in php.ini or with date_default_timezone_set()).

It's currently BST in the UK and without calling setTimeZone() on a DateTime object, format() will produce a date/time that is one hour behind.

If setTimeZone() is called on a DateTime object then the date/time produced by format() is correct.

date() by itself uses the default timezone that has been set.  For consistency, DateTime should do also.

Test script:
---------------
<pre>
<?php
	date_default_timezone_set('Europe/London');

	$now = time();

	// Initialising with a timestamp, second DateTimeZone parameter would be ignored.
	$dt1 = new DateTime('@' . $now);
	echo 'DateTime->format()', "\t", $dt1->format('Y-m-d H:i:s T Z e');
	echo "\n";

	$dt2 = new DateTime('@' . $now);
	$dt2->setTimeZone(new DateTimeZone('Europe/London'));
	echo 'DateTime->format()', "\t", $dt2->format('Y-m-d H:i:s T Z e');
	
	echo "\n";
	echo 'date()', "\t\t\t", date('Y-m-d H:i:s T Z e', $now);
?>
<pre>

Expected result:
----------------
$dt1->format() should use Europe/London as the timezone and show the correct time for that timezone.

Actual result:
--------------
$dt1->format() shows +00:00 as the timezone and is an hour behind.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-09-08 17:03 UTC] derick@php.net
-Status: Open +Status: To be documented
 [2011-09-08 17:03 UTC] derick@php.net
This bug has been fixed in SVN.

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.

@ sets the timezone to "UTC", this should be documented at http://uk3.php.net/manual/en/datetime.formats.compound.php
 [2011-09-12 10:37 UTC] bjori@php.net
-Status: To be documented +Status: Open
 [2011-09-12 10:37 UTC] bjori@php.net
-Type: Bug +Type: Documentation Problem
 [2012-02-25 16:29 UTC] salathe@php.net
Automatic comment from SVN on behalf of salathe
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=323519
Log: mention that @ sets the timezone to UTC (doc #55642)
 [2012-02-25 16:30 UTC] salathe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: salathe
 [2012-02-25 16:30 UTC] salathe@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC