Re: Timezone date/time conversion

From: Date: Mon, 01 Aug 2005 23:14:41 +0000
Subject: Re: Timezone date/time conversion
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-219801@lists.php.net to get a copy of this message
I'm sure something like this has already been written and I'd hate to redesign the wheel. I've searched google but came up empty and am wondering if any of you guys have what I am looking for.
I ended up writing my own. Hopefully to help anyone else out trying to do the same thing, I've included my function below. I used the following page as a guide http://www.timeanddate.com/library/abbreviations/timezones/ function calculateTimeZoneDate( $dateTime, $fromTZ, $toTZ, $fromLocation = 'North America', $toLocation = 'GMT' ) {
 $retval     = $dateTime;
$timeStamp = strtotime( $dateTime ); $timeZonesArray = array( 'GMT' => array( 'GMT' => +0 // GMT
                                                     ),
                          'North America'  => array( 'NST' => -3.5, // Newfoundland Standard Time
                                                     'NDT' => -2.5, // Newfoundland Daylight Time
                                                     'AST' => -4, // Atlantic Standard Time
                                                     'ADT' => -3, // Atlantic Daylight Time
                                                     'EST' => -5, // Eastern Standard Time
                                                     'EDT' => -4, // Eastern Daylight Time
                                                     'CST' => -6, // Central Standard Time
                                                     'CDT' => -5, // Central Daylight Time
                                                     'MST' => -7, // Central Daylight Time
                                                     'MDT' => -6, // Mountain Daylight Time
                                                     'PST' => -8, // Pacific Standard Time
                                                     'PDT' => -7, // Pacific Daylight Time
                                                     'AKST' => -9, // Alaska Standard Time
                                                     'AKDT' => -8, // Alaska Daylight Time
                                                     'HAST' => -10, // Hawaii-Aleutian Standard Time
                                                     'HADT' => -9 // Hawaii-Aleutian Daylight Time
                                                     ),
                          'Australia'      => array( 'NFT' => +11.5, // Norfolk (Island) Time
                                                     'EST' => +10, // Eastern Standard Time
                                                     'EDT' => +11, // Eastern Daylight Time
                                                     'CST' => +9.5, // Central Standard Time
                                                     'CDT' => +10.5, // Central Daylight Time
                                                     'WST' => +8, // Western Standard Time
                                                     'CXT' => +7, // Christmas Island Time
                                                     ),
                          'Europe'         => array( 'GMT' => +0, // Greenwich Mean Time
                                                     'BST' => +1, // British Summer Time
                                                     'IST' => +1, // Irish Summer Time
                                                     'WET' => +0, // Western European Time
                                                     'WEST' => +1, // Western European Summer Time
                                                     'CET' => +1, // Central European Time
                                                     'CEST' => +2, // Central European Summer Time
                                                     'EET' => +2, // Eastern European Time
                                                     'EEST' => +3 // Eastern European Summer Time
                                                     ),
                          'Military'       => array( 'Z' => +0, // Zulu Time Zone
                                                     'Y' => -12, // Yankee Time Zone
                                                     'X' => -11, // X-ray Time Zone
                                                     'W' => -10, // Whiskey Time Zone
                                                     'V' => -9, // Victor Time Zone
                                                     'U' => -8, // Uniform Time Zone
                                                     'T' => -7, // Tango Time Zone
                                                     'S' => -6, // Sierra Time Zone
                                                     'R' => -5, // Romeo Time Zone
                                                     'Q' => -4, // Quebec Time Zone
                                                     'P' => -3, // Papa Time Zone
                                                     'O' => -2, // Oscar Time Zone
                                                     'N' => -1, // November Time Zone
                                                     'A' => +1, // Alpha Time Zone
                                                     'B' => +2, // Bravo Time Zone
                                                     'C' => +3, // Charlie Time Zone
                                                     'D' => +4, // Delta Time Zone
                                                     'E' => +5, // Echo Time Zone
                                                     'F' => +6, // Foxtrot Time Zone
                                                     'G' => +7, // Golf Time Zone
                                                     'H' => +8, // Hotel Time Zone
                                                     'I' => +9, // India Time Zone
                                                     'K' => +10, // Kilo Time Zone
                                                     'L' => +11, // Lima Time Zone
                                                     'M' => +12 // Mike Time Zone
                                                     ));
$fromGMTDiff = $timeZonesArray[$fromLocation][$fromTZ];
 $toGMTDiff    = $timeZonesArray[$toLocation][$toTZ];
if(( '' != trim( $fromGMTDiff )) && ( '' != trim( $toGMTDiff ))) { if( $fromGMTDiff > $toGMTDiff ) {
     $netDiff = $fromGMTDiff - $toGMTDiff;
} else {
     $netDiff = $toGMTDiff - $fromGMTDiff;
} $retval = date( 'Y-m-d h:i:sa', ( $timeStamp + ( 3600 * $netDiff ))); } return $retval; } // end function calculateTimeZoneDate() As for daylight savings time, I'm leaving it up to the developer using the above function to determing the appropriate TZ based on DST to pass in. I didn't want to have to worry about what timezones actually use DST. If anyone sees any gross mistakes, please let me know. Other- wise, please feel free to use if needed. thnx, Chris

« previous php.general (#219801) next »