如何从PHP的IP地址/国家/地区名称中找到时区ID? [英] How to find timezone ID from IP address/country name in PHP?

查看:404
本文介绍了如何从PHP的IP地址/国家/地区名称中找到时区ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我,PHP是否可以通过IP地址或国家/地区名称获取时区(例如'Asia/Calcutta')"?

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?

说明:

我正在尝试根据他/她所在的国家/地区设置用户时区.我正在从他的IP地址获取用户所在的国家/地区,但是我需要该国家/地区的时区,例如('Asia/Calcutta'),以检查该国家/地区是否已启用DST. 谁能为我提供一些适合的PHP解决方案?

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?

推荐答案

使用IP地址

时区有时遵循古怪的规则. IP地理定位绝不是精确的(尽管有些供应商声称如此).

Timezones follow sometimes quirky rules. IP geolocation is anything but precise (notwithstanding the claims of some vendors).

话虽如此,您当然可以使用MaxMind之类的Geo IP产品找到城市和国家/地区:

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

其中包含一个PHP模块

which includes a PHP module

http://www.maxmind.com/app/php

然后您可以使用MaxMind API尝试估算用户的时区

You can then use the MaxMind APIs to try and estimate the user's timezone

http://www.maxmind.com/app/faq#timezone

替代

如果您想依靠用户的时钟而不是IP地址,则jsTimezoneDetect可以很好地工作(尽管并不完美).

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

摘要

这两种技术在所有情况下均无法完美运行.确保您允许用户更正任何自动生成的时区建议.

Neither technique works perfectly in all cases. Be sure you allow your user to correct any auto-generated timezone suggestion.

这篇关于如何从PHP的IP地址/国家/地区名称中找到时区ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆