Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find timezone ID from IP address/country name in PHP?

Tags:

timezone

php

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?

like image 610
Amarjeet Kaur Avatar asked Jun 27 '12 04:06

Amarjeet Kaur


1 Answers

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.

like image 142
Eric J. Avatar answered Nov 03 '22 01:11

Eric J.