note 79596 rejected from function.gmmktime by danbrown

From: Date: Sun, 20 Jul 2008 03:17:17 +0000
Subject: note 79596 rejected from function.gmmktime by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142042@lists.php.net to get a copy of this message
Note Submitter: Glen ---- 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?

« previous php.notes (#142042) next »