A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

Get number of hours between 2 timezones

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

This entry was posted in General and tagged , . Bookmark the permalink.

7 Responses to Get number of hours between 2 timezones

  1. Brian says:

    Is it possible to get the current time for Lexington without setting the default timezone?

  2. Wenbert says:

    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

  3. Brian says:

    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.

  4. Wenbert says:

    yes i would recommend using the first example. i haven’t really dealt with daylight saving time seriously before… hehe

  5. G Barr says:

    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

  6. Vitaly says:

    Thank you, found it very useful!

  7. Vitaly says:

    And yeah, this font for replies in comments is really impossible to read :).
    Though the post was of much use to me!)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>