note 84640 modified in function.date-create by felipe

From: Date: Wed, 23 Jul 2008 12:57:21 +0000
Subject: note 84640 modified in function.date-create by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142590@lists.php.net to get a copy of this message
In case you have on server, which you don't administer, version of php < 5.2 and need timezone conversion similar to DateTime, you can use this: <?php class DateUtils { public static function serverTime2ForeignTime($serverTimeStamp, $timezoneToTransferTo = "Europe/Prague") { // get local int representation of time $localTimestamp = strtotime($serverTimeStamp); // get far away places int representation of time $timezoneTimestamp = strtotime($serverTimeStamp . ' ' . $timezoneToTransferTo); // get difference of times $timezoneDifference = $timezoneTimestamp - $localTimestamp; // ammend local time to represent the same second as time in far away place return date("Y-m-d H:i:s", $localTimestamp - $timezoneDifference); } } //$timezoneToTransferTo should be in Olsen format. ?> It passed our set of unit tests generated using DateTime + DateTimeZone (we use just 76 zones, but don't have a reason to think it wont work for all) --was-- In case you have on server, which you don't administer, version of php < 5.2 and need timezone conversion similar to DateTime, you can use this : class DateUtils { public static function serverTime2ForeignTime($serverTimeStamp, $timezoneToTransferTo = "Europe/Prague") { // get local int representation of time $localTimestamp = strtotime($serverTimeStamp); // get far away places int representation of time $timezoneTimestamp = strtotime($serverTimeStamp . ' ' . $timezoneToTransferTo); // get difference of times $timezoneDifference = $timezoneTimestamp - $localTimestamp; // ammend local time to represent the same second as time in far away place return date("Y-m-d H:i:s", $localTimestamp - $timezoneDifference); } } //$timezoneToTransferTo should be in Olsen format. It passed our set of unit tests generated using DateTime + DateTimeZone (we use just 76 zones, but don't have a reason to think it wont work for all) http://php.net/manual/en/function.date-create.php

« previous php.notes (#142590) next »