Sunday, February 6, 2011

PHP gmtime?

I've seen some forum talking about, how nice to have gmtime() function.
But according to some post, it seems that time() is already in gmtime.

But how do we convert a unixtimestamp stored via gmdate("Y-m-d H:i:s") to a offset where its not the server locale timezone?

Its kind of confusing.
But the trick i used was to add the offset to the timestamp, and check if daylight is in effect.
Example of code:
$gmtime = $oRow["ftime"];
$daylight = date("I", $gmtime);
$iOffsetHour = _user_timezone_offset_here;
if ($daylight){
if ($iOffsetHour > 0){ $iOffsetHour-=1; }
else { $iOffsetHour+=1; }
}
$iLocalTime = $gmtime + (60*60*$iOffsetHour);

And to get a quick gmtime():
$gmtime = strtotime(gmdate("Y-m-d H:i:s"));

If anyone find this wrong, please correct me.
thank you :)

2 comments:

Garen said...

I use this to get a GMT unix-style time stamp:

$tmnow = date("U",mktime(gmdate("H"), gmdate("i"), gmdate("s"), gmdate("m"), gmdate("d"), gmdate("Y") ));

Unknown said...

Wow , thats some code to get gmt time...