Can anyone please tell me if there is any way available in PHP to fetch timezone region like('Asia/Calcutta') from IP Address or country name?
Description:
I am trying to set user timezone based on his/her country. I am getting user country from his IP address, But I need timezone region of that country like('Asia/Calcutta') to check whether DST is on in that country. Can anyone please provide me some suitable solution in PHP?
I consider a better idea is to finding out which timezone is set based on the user client setting. Maybe there is the situation that a user is in a timezone, which he doesn't want to be.
One simple possibility is to ask the user, but that's to easy.
With JavaScript you could find out the offset in minutes from UTC. Also you could find out, if the client is in a country with daylight saving time (DST) and if dst is active. This could be done, by comparing the time of two date objects, one with the month
January
and one with the monthJuly
.Read the cookie with PHP and interpreting the information of them with timezone_name_from_abbr():
This way you get time zone..
If you want to do it on the server side, you can do this with PHP:
Note that this is synchronous.
Alternatively, if you can do it on the client side. You can use this with jQuery or equivalent:
Disclaimer: I run IP Sidekick.
Using IP Address
Timezones follow sometimes quirky rules. IP geolocation is anything but precise (notwithstanding the claims of some vendors).
Having said that, you can certainly find the city and country using a Geo IP product such as MaxMind's:
http://www.maxmind.com/app/geolite
which includes a PHP module
http://www.maxmind.com/app/php
You can then use the MaxMind APIs to try and estimate the user's timezone
http://www.maxmind.com/app/faq#timezone
Alternative
If you want to rely on your user's clock rather than the IP address, jsTimezoneDetect works quite well (though it is not perfect).
https://bitbucket.org/pellepim/jstimezonedetect/wiki/Home
Summary
Neither technique works perfectly in all cases. Be sure you allow your user to correct any auto-generated timezone suggestion.