Convert date time data between php servers and user browsers

You need this because:

  • Your web server is in a fixed location when your users can be in any country all over the world,
  • Our world doesn’t use the same clock but it’s divided in to 24 parts called timezones
  • You need to get, manipulate or display current date time between your web server and your user’s browser

Timezone, UTC time and local time

In general, we have this formula to calculate local time

local time = UTC time + your timezone (in hour)

For example, I’m in Vietnam, my timezone is UTC+7, so when the UTC time is 00:00, my clock will display current time as 07:00. It is so easily to reason the following lines out:

(A) local time = (B) local time – (B) timezone + (A) timezone

PHP function

To implement what we found in PHP, I use this code snippet:

function convert_from_another_time($source, $source_timezone, $dest_timezone){
    $offset = $dest_timezone - $source_timezone;
    if($offset == 0)
        return $source;
    $target = new DateTime($source->format(Y-m-d H:i:s));
    $target->modify($offset+' hours');
    return $target

In above function:

  • $source: a DateTime object contains our source data
  • $source_timezone: an integer refers to the timezone of our source data. For example, if the timezone of our source data is ‘Asia/Ho_Chi_Minh’ (UTC+7), $source_timezone is 7.
  • $dest_timezone: an integer refers to the timezone of our target data

As you can see, in my function, I create a new DateTime object and “clone” the data of $source to it. This ensure our old value will not be changed after executing.

Get the timezone of your php web server

Use this code:

$d = new DateTime();
$dtz = ($d->getOffset())/3600;
// we can also use the this procedure style
// $dtz = date_offset_get($d);

Notice that the getOffset method return the difference between your local web server time and UTC time in second, you have to change this value to hours before pass to our convert function.

Get the timezone of your user

The only way to do this is use a client script language like javascript. You can consider using my code:

var d = new Date();
var dtz = -(d.getTimezoneOffset())/60;

Why,i use the negative operation before the result? Unlike php, javascript getTimezoneOffset function return the difference between the UTC time and the client time in minute, therefore, you need to convert that value like above.

Let you server know the user timezone

This is what i used:

  • PHP: check if there is a timezone data in the cookie.
  • JS: if there is no timezone data, calculate like previous session and make an ajax call with post data contains the result.
  • PHP: set the client cookie.

The last thing for WAMP Server 2 users

If you use WAMP Server 2, It’s highly recommended to review your date.timezone value in the php.ini file. In my experience, WAMP makes your php timezone as UTC when keep the timezone of your MySQL the same as your local timezone by default. This may cause further troubles when working with DATETIME, DATE, TIME or TIMESTAMP fields.

5 thoughts on “Convert date time data between php servers and user browsers

  1. @kenko: if your PHP version is newer than 5.3.0 you can use this code:
    $date = DateTime::createFromFormat(“m d Y H:iA”,”02 08 2012 10:09PM”);
    $date->add(new DateInterval(“P61DT95M”));
    echo $date->format(“Y-m-d H:i:s”);
    Change the string in [mono]add[/mono] method to your own. Read more [url=]here[/url]

  2. I have created a guestbook in PHP which dispays the time with diference of 5 hours less.
    The function I used is:
    By using the following, I can view it correctly, but not get printed on guestbook entries.
    date_default_timezone_set(“Europe/Lisbon”); echo(“d/m/Y-H:i”);
    Any help?

  3. There is an easier way: use the javascript library ‘moment.js’ to get all time in UTC.
    That way there can be no confusion, and you will always compare UTC times with UTC times

    In PHP you can use the ‘gmdate’ function to determine the UTC time on your server

Leave a Reply

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