如何通过传递国家代码或国家名称在php中获取时区

如何通过传递国家代码或国家名称在php中获取时区,php,Php,我有使用一些api的国家代码和国家名称,是否有任何方法可以在php中获得给定国家代码和国家名称的时区。我有这样的数据 address:Object city:"-" continent:"Asia" continent_code:"AS" country:"India" country_code:"IN" 我得到这个数据,我想得到给定地址的时区,有什么方法可以在php中实现 提前感谢PHP有一个使用GeoIP的函数 geoip\u time\u zone\u by\u country\u an

我有使用一些api的国家代码和国家名称,是否有任何方法可以在php中获得给定国家代码和国家名称的时区。我有这样的数据

address:Object
city:"-"
continent:"Asia"
continent_code:"AS"
country:"India"
country_code:"IN"
我得到这个数据,我想得到给定地址的时区,有什么方法可以在php中实现


提前感谢

PHP有一个使用GeoIP的函数

geoip\u time\u zone\u by\u country\u and\u region()
函数将返回与国家和地区代码组合对应的时区

$timezone = geoip_time_zone_by_country_and_region('CA', 'QC');
if ($timezone) {
    echo 'Time zone for CA/QC is: ' . $timezone;
}

您需要下载并包含GeoIP库

如果您只有国家名称信息,则可能无法反向查找所有时区


例如,美国有5个不同的时区。这是一种一对多关系。

如果您不想安装PECL库

您可以使用DateTimeZone:


$timezone=\DateTimeZone::listIdentifiers(\DateTimeZone::PER\u COUNTRY,$countryCode)

可能的重复您只需自己创建一个查找表;但是,在澳大利亚或美国这样跨越多个时区的情况下,你会怎么做?最好使用正确的标识。我尝试了你的代码,但我收到了未定义函数geoip\u time\u zone\u的错误调用,该函数由\u country\u和\u region()在你的站点中包含geoip<代码>http://dev.maxmind.com/geoip/legacy/geolite/
我已经更新了我的答案,加入了linkI getting likeArray([0]=>America/Adak[1]=>America/Anchorage[2]=>America/Boise[3]=>America/Chicago[4]=>America/Denver[5]=>America/Detroit[6]=>America/Indiana/Indianapolis[7]=>America/Indiana/Knox[8]=>America/Indiana/Marengo[9]=>America/Indiana/Petersburg[10]=>America/Indiana/Tell_City[11]=>America/Indiana/Vevay[12]=>America/Indiana/Vincennes[13]=>America/Indiana/Winamac[14]=>America/Juneau[15]=>America/Kentucky/Louisville[16]=>America/Kentucky/Monticello[17]=>…当使用美国的国家代码时,使用您的solution@SayedMohdAli使用国家iso2