|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 00:00:01 2025 UTC |
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.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.