note 79596 added to function.gmmktime

From: Date: Wed, 05 Dec 2007 00:27:42 +0000
Subject: note 79596 added to function.gmmktime
Groups: php.notes 
Request: Send a blank email to php-notes+get-132827@lists.php.net to get a copy of this message
I need a multi-timezone system, and the best way is to store and do calculations on all times in GM/UTC. When outputting times, do the conversion to a local time then. So my challenge was to figure out how to get a UTC timestamp in the first place. That's the equivalent of time() .. but returning in UTC. I innocently thought such a routine must exist in PHP to do likewise. It doesn't. This routine just converts UTC time to local time (adds/subtracts your timezone) from the parameters you pass. Without parameters, it is the same as other routines (pretty useless then): <?php $mt = mktime(); // gives you local time $gt = gmmktime(); // gives you local time $t = time(); // gives you local time echo "mktime = " . mktime() . "<br />"; echo "gmmktime = " . gmmktime() . "<br />"; echo "time = " . time() . "<br />"; ?> returns: mktime = 1196812966 gmmktime = 1196812966 time = 1196812966 How do you get a UTC timestamp then ? <?php $utc_str = gmdate("M d Y H:i:s", time()); $utc = strtotime($utc_str); ?> you have to get the current timestamp, convert it to a GM/UTC formatted string, then convert it back to a timestamp. You have to make sure you sure 24hr format. Another way is to manually adjust the current time by your computer's timezone offset, but figuring out the offset isn't exactly a brain-dead operation either. Why is this so hard? ---- Server IP: 64.71.164.2 Probable Submitter: 68.122.118.112 ---- Manual Page -- http://www.php.net/manual/en/function.gmmktime.php Edit -- https://master.php.net/note/edit/79596 Del: integrated -- https://master.php.net/note/delete/79596/integrated Del: useless -- https://master.php.net/note/delete/79596/useless Del: bad code -- https://master.php.net/note/delete/79596/bad+code Del: spam -- https://master.php.net/note/delete/79596/spam Del: non-english -- https://master.php.net/note/delete/79596/non-english Del: in docs -- https://master.php.net/note/delete/79596/in+docs Del: other reasons-- https://master.php.net/note/delete/79596 Reject -- https://master.php.net/note/reject/79596 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#132827) next »