Special Tip
Confused by using
gmdate() and still getting wrong time? It's happening thanks to daylights saving time and wheter the server is in summer or winter time zone. How to fix that and
ALWAYS get the proper base date for all calculations?
<?
function GMT_Time() {
return gmdate("U") + date("Z");
}
?>
|
As you can see, this simple function returns the proper GMT
Timestamp, so to use it in any following code you need something like this:
<?=
gmdate("H:i", GMT_Time())
?>
|
Updated (14th April 2005)
Last time we've forgotten a quite important use for our script -
input timestamp
correction. What if we already have our own timestamp and just want to make sure it's correct? Let's modify our function a little bit
<?
# ~~~ GMT Time Magic v2.0 ~~~~~~~~~~ #
function GMT_Time($Stamp) {
return (($Stamp == 0) ? time() : $Stamp) + date("Z");
}
?>
|
Our new GMT Time Magic might look a bit confusing thanks to advanced use of
if/else statement, but don't get disgusted. If you take a closer look, you can see by reading from left to right the following:
IF the incoming
$Stamp IS 0, generate a new timestamp,
ELSE if the incoming
$Stamp IS NOT 0 (reverse of the previous statement), use the original $Stamp value. Then add the daylight saving time modification and return the string. Simple enough, huh?
<?=
gmdate("H:i", GMT_Time(0))
?>
|
- or -
<?=
gmdate("H:i", GMT_Time($My_Stamp))
?>
|
It's exactly the same script as the one used on this page, so if there's a bug, you can find it out yourself very easily. For any comments, suggestions or bugreports, please visit our
message board and leave your note.
Updated (18th July 2005)
One more place for confusion discovered. There is a difference if you are making the stamp (
mktime) or reading it (
date). Also if your server has offset 0 (GMT Zone), you don't need any of this, because
gmdate() and
gmmktime() work perfectly. So let's see the last version, exactly as used here, no more theories ;)
Note: In this script the incomming timestamp condition is not set to 0, because if you used 0 in the conversion tool it would return the actual time instead of beginning of the epoch. But otherwise its useless, just a visual game.
<?
# ~~~ GMT Time Magic v2.3 ~~~~~~~~~~ #
function GMT_Time($Stamp=0,$Change=0) {
return ((!$Stamp) ? time() : $Stamp) - $Change * date("Z");
}
?>
|
And the final calling code for both commands (number 1 is your server's offset)
<?=
date("l, F jS Y, H:i:s", GMT_Time($Timestamp, 1) + 3600 * $Timezone)
?>
|
- and -
<?=
GMT_Time($Timestamp, (-1)) + 3600 * $Timezone
?>
|
- src -