php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39012 Incorrect gmdate('U') and date_default_timezone_set()
Submitted: 2006-10-01 19:57 UTC Modified: 2006-10-05 09:29 UTC
From: djmaze at planet dot nl Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.1.6 OS: Windows
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: djmaze at planet dot nl
New email:
PHP Version: OS:

 

 [2006-10-01 19:57 UTC] djmaze at planet dot nl
Description:
------------
gmdate('U') shows the incorrect unix epoch.

http://php.net/gmdate: 'Format a GMT/UTC date/time'
http://php.net/date: 'U = Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)'

After calling date_default_timezone_set() everything is messed up, or is that the whole purpose but someone forgot to document it?


Reproduce code:
---------------
<?php
function get_unixts()
{
	return array(
	'time()'   => time(),
	'date(\'U\')' => date('U'),
	'date(\'Z\')'  => date('Z'),
	'gmtime' => (time()-date('Z')),
	'gmdate(\'U\')' => gmdate('U'),
	'gmdate(\'Z\')' => gmdate('Z'),
	'date_default_timezone' => date_default_timezone_get(),
	);
}
$def = get_unixts();
date_default_timezone_set('UTC');
$set = get_unixts();

echo '<html><body><table border="1"><tr><td>type</td><td>normal</td><td>after set UTC</td></tr>';
foreach ($def as $t => $v)
{
	echo "<tr><td>$t</td><td>$v</td><td>{$set[$t]}</td></tr>";
}
echo '</table></body></html>';
?>

Expected result:
----------------
type        normal      after set UTC
time()	    1159732108  1159724908
date('U')   1159732108  1159724908
date('Z')   7200        0
gmtime      1159724908  1159724908
gmdate('U') 1159724908  1159724908
gmdate('Z') 0           0


Actual result:
--------------
type        normal      after set UTC
time()	    1159732108  1159732108
date('U')   1159732108  1159732108
date('Z')   7200        0
gmtime      1159724908  1159732108
gmdate('U') 1159732108  1159732108
gmdate('Z') 0           0


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-02 09:41 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Unix timestamps are always in GMT (=UTC) so there is no problem here.
 [2006-10-02 18:05 UTC] djmaze at planet dot nl
You are contradicting.
Quote: Unix timestamps are always in GMT (=UTC) so there is no problem here.

My server is set to CET which is currently GMT+2
time()-date('Z') = 1159724908 = UTC
gmdate('U') = 1159732108 = GMT+2

As you can see gmdate('U') is NOT UTC

I found so many more issues with the current date and time functions that i'm planning to write my own php_utc extension with utcdate() and utctime() functions with an explanation why.
 [2006-10-02 18:44 UTC] derick@php.net
time() also returns a unix timestamp. A unix timestamp is always in UTC so it is pointless to do -date('Z') there is that would shift the time two hours into the past.
time() and gmdate('U') show the same time, and that is correct.

But feel free to make your own functions which implement the wrong logic.
 [2006-10-05 09:29 UTC] djmaze at planet dot nl
Thanks got it. It seems i've misinterpreted the time() function for years after some commented that time() was local time and not gmt.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 20:01:33 2025 UTC