Jump to content

Detection of time zone in PHP


frik

Recommended Posts

Hi folks,

 

My web server is (I think) in Europe. My scripts that run on it don't currently know automatically what the

locality is of the user who visits the website.

 

How can I make sure that my PHP code displays the right time for the user?

 

Also, I store time/date in my database using GMT unix time. How can I always convert that

to a string that is localized to my visitors?

 

Thanks.

 

Link to comment
Share on other sites

There is nothing in a standard HTTP request that will tell you what the user's timezone is. You either need to ask them for it then save it somewhere, or you need to use Javascript to query for it from the browser.

 

I tend to just output all dates and times in UTC and then on page load use Javascript to convert them all into the user's local timezone. If they have JS enabled, they will see the proper time. If they don't, they will still see the UTC time at least. If you also display the current time somewhere on the page, they will be able to see what the difference is between the time displayed and their local time.

Link to comment
Share on other sites

There is nothing in a standard HTTP request that will tell you what the user's timezone is. You either need to ask them for it then save it somewhere, or you need to use Javascript to query for it from the browser.

 

I tend to just output all dates and times in UTC and then on page load use Javascript to convert them all into the user's local timezone. If they have JS enabled, they will see the proper time. If they don't, they will still see the UTC time at least. If you also display the current time somewhere on the page, they will be able to see what the difference is between the time displayed and their local time.

 

Is there a simple JS script that will do this conversion, or would I need to import a bunch of code?

 

Also what about services that identify time zone based on IP?

Link to comment
Share on other sites

Is there a simple JS script that will do this conversion, or would I need to import a bunch of code?

If you only care about the local timezone and you can output a unix timestamp then it's relatively simple.

Example fiddle

    var timestamp = 1423419743; //Have PHP provide this value.
    var dt = new Date(timestamp*1000);
    var formatted = (dt.getMonth()+1) + '/' + dt.getDate() + '/' + dt.getFullYear() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds();
    console.log(formatted);
With a little extra work you could remove the need to output the timestamp and use the date string instead:

Example Fiddle

    
    var datetimestr = '2015-02-08 18:22:23'; //Have PHP provide this value somehow
    var matches=datetimestr.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2})\d{2})\d{2})/);    
    var dt = new Date();
    dt.setUTCFullYear(matches[1]);
    dt.setUTCMonth(matches[2]-1);
    dt.setUTCDate(matches[3]);
    dt.setUTCHours(matches[4]);
    dt.setUTCMinutes(matches[5]);
    dt.setUTCSeconds(matches[6]);

    var formatted = (dt.getMonth()+1) + '/' + dt.getDate() + '/' + dt.getFullYear() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds();
    console.log(formatted);
You'll probably want to make formatted a little nicer, but it's generally not a lot of work/code.

 

If you want to get more elaborate and be able to do easier formatting, multiple timezones, etc then you'd want to look into a library such as moment.js and moment-timezone.js

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.