Here is a way to get the number of hours between two different timezones. I am sure that there is a way of doing it shorter and faster, but I did it this way for readability purposes.
date_default_timezone_set("America/Kentucky/Louisville"); $lex = date("Y-m-d h:i:s A"); //Lexington, USA echo "Lex: ".date("Y-m-d h:i:s A")."nn"; date_default_timezone_set("Asia/Hong_Kong"); $cebu = date("Y-m-d h:i:s A"); //Cebu, Philippines echo "Cebu: ".date("Y-m-d h:i:s A")."nn"; echo (strtotime($cebu) - strtotime($lex))/3600 ." hours difference between the timezones"; //3,600 seconds is 1 hour |
List of suported timezones in PHP can be found here:
http://us2.php.net/manual/en/timezones.php
Is it possible to get the current time for Lexington without setting the default timezone?
Hi brian,
You could get the current time of the server and then add or subtract hours so that it will coincide with the time in Lexington.
For example, let’s say that the server time in your web hosting company is 7:30 AM, and then in Lexington, it’s 9:30AM, so all you have to do is to get the difference between the two times. 9 minus 7 is 2 (hours).
So I would do something like this:
$var = strtotime(date(‘Y-m-d H:i:s’))+7200; //i get the current date and convert it into Unix Timestamp. then i add 7200 seconds = 2 hours.
//Google “2 hours to seconds”
echo date(‘Y-m-d H:i:S’,$var); //this would convert the Unix Timestamp back to readable time
thanks bai. However if one of the timezones uses daylight saving time you’d also have to code for that. I guess the first example you posted would be a much easier way to get the current time of another timezone.
yes i would recommend using the first example. i haven’t really dealt with daylight saving time seriously before… hehe
It is always better (safer) to use the time zones from the server to calculate the difference bewteen time zones. Not only will it handle day light savings but also any changes to this in the future. There is for instance arguements every year in the UK if day light saving should be used.
regards
George
Thank you, found it very useful!
And yeah, this font for replies in comments is really impossible to read :).
Though the post was of much use to me!)