Solving time difference between hosting server and local timezone in PHP

I think this is one of the common problem you’ll face while you’re working for the website of a particular country and web server is located in another different country. If client have the dedicated server then you won’t face this problem. But if you just own a single account of that server then you’ll most probably face this problem of synchronizing the time difference of server time and local time.

Problem of time difference of remote server and local time

I was working for Australian auction project in the beginning of my career and the client’s server was located in USA. I was using date() function to get the current date and time in my code.But, it didn’t work at all. You know the reason, I was taking the current time of USA and auction was closing according to timezone of USA, not according to Australian Time Standard. After that, I realized that I should get the local time according to website no matter where the server is hosted.

How to get the local time from remote server in PHP?

Whenever you need to get the current local time and local date in your code then you must take the reference from gmdate() function available in PHP. This function returns the GMT date or time for you. You must know the time difference between GMT and your local time to be used in the project.And, adding that difference to GMT, will give you exact local time for you in PHP no matter where the web server is located.

Function to find the current local time in PHP

Well, I’ve just written a function in PHP which will get the current local time or local date for your project, no matter where the web server related to your project is hosted.

First of All, we must define the hour and second difference from GMT with your current local time in configuration file of your project.

 define("GMT_HR_DIFF",5);
 define("GMT_MIN_DIFF",45);

As, you can see above the the time difference from GMT is 5 hour and 45 second which is Nepali Standard time(NST).
Now, let’s look at the function to reture the current local NST in PHP no matter where the server is hosted.”2008-04-14″ is the example of the date format returned by the function and “2008-04-14 12:15:03 is the format of the time returned by the function and these are the standard time format to be stored in the database.

function getCurrentTime($flag='time')
{
   if($flag=='time')
    return date("Y-m-d H:i:s",mktime (gmdate("H")+GMT_HR_DIFF,gmdate("i")+GMT_MIN_DIFF,gmdate("s"),gmdate("m"),gmdate("d"),gmdate("Y")));
   else
    return date("Y-m-d",mktime(gmdate("H")+GMT_HR_DIFF,gmdate("i")+GMT_MIN_DIFF,gmdate("s"),gmdate("m"),gmdate("d"),gmdate("Y")));
}

As you can see above, I’ve taken the help gmdate() and mktime() function, which returns the UNIX timestamp for date.The hour and minute difference is added to GMT time to get the current local time or date for you. If you call the function without any parameter, then this function returns the current time and whenever you call this function with the other parameter like “getCurrentTime(‘date’)”, then this function returns the current date according to the timezone set in two parameter.

7 thoughts on “Solving time difference between hosting server and local timezone in PHP

  1. Why not just use
    putenv(‘TZ=America/Los_Angeles’);
    to change the timezone?

    Your solution is a bit too much for the problem.

    Z

  2. Zaft, I agree with you this is a bit long process but reliable one.

    There are lots of problem with “TZ” environment variable check this
    http://www.php.net/putenv

    And, even sometime the format of the “TZ” varible ma y vary into various UNIX system. And furthermore, you can only use those “TZ” variables which are defined in your particular system, it may or may not work with the other system

  3. Sweet! Thanks alot!

  4. Nice help

  5. track

    good thread

  6. Parag chaure

    This is good, but for this we have to know the proper standard GMT, It will better for set the time zone by using date_default_timezone_set() function date_default_timezone_set(America/Los_Angeles);

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=""> <s> <strike> <strong>