More timezone problems - a solution?
Posted: Wed Feb 28, 2007 12:56 am
I've been grappling with this on and off all day. The issue of formatting a given timestamp to another timezone.
This is the current solution. It seems to adjust the time and factor in daylight savings etc.
Can anyone see any problems or suggest anything better?
With this I can call :-
and get :-
This is the current solution. It seems to adjust the time and factor in daylight savings etc.
Can anyone see any problems or suggest anything better?
Code: Select all
function tz_date($format, $timestamp = false, $target_tz = false) {
if(!$timestamp) $timestamp = time(); // No timestamp specified, use current time
if($target_tz) { // If timezone is specified
$local_tz = getenv("TZ"); // Get the original Timezone
if(!($local_tz == $target_tz)) { // If timezone shift required
putenv("TZ=$target_tz"); // Set timezone accordingly
$result = date($format, $timestamp); // Capture localized date/time
putenv("TZ=" . $local_tz); // Set timezone to original state
return $result; // Return the value
}
}
return date($format, $timestamp); // Return local time - timezone unspecified or matches
}Code: Select all
echo tz_date('H:i:s') . "(Server) - ";
echo tz_date('H:i:s', false, "Etc/GMT") . "(GMT) - ";
echo tz_date('H:i:s', false, 'Australia/Melbourne') . "(Melbourne) - ";
echo tz_date('H:i:s') . "(Server)<br><br>";
echo tz_date('H:i:s', "1172641973") . "(Server) - ";
echo tz_date('H:i:s', "1172641973", "Etc/GMT") . "(GMT) - ";
echo tz_date('H:i:s', "1172641973" ,'Australia/Melbourne') . "(Melbourne)<br><br>";Cheers all16:20:12(Server) - 06:50:12(GMT) - 17:50:12(Melbourne) - 16:20:12(Server)
15:22:53(Server) - 05:52:53(GMT) - 16:52:53(Melbourne)