使用国家/地区名称或城市名称在国家/地区获取时间(如果国家/地区有多个时区,可以获得更准确的时间)

时间:2012-08-30 19:23:45

标签: php api time

在国家/地区使用国家/地区名称或城市名称获取时间(如果国家/地区有多个时区,可以获得更准确的时间)

例如

echo get_local_time('England');  //8:23pm
echo get_local_time('New York'); //7:23am 

这可能纯粹是用PHP /那里有任何API吗?

1 个答案:

答案 0 :(得分:9)

这是使用google api和php从国家/地区名称和城市名称获取时间的代码。

<?php
function get_timee($country,$city)
{
$country = str_replace(' ', '', $country);
$city = str_replace(' ', '', $city);
$geocode_stats = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=$city+$country,&sensor=false");
$output_deals = json_decode($geocode_stats);
$latLng = $output_deals->results[0]->geometry->location;
$lat = $latLng->lat;
$lng = $latLng->lng;    
$google_time = file_get_contents("https://maps.googleapis.com/maps/api/timezone/json?location=$lat,$lng&timestamp=1331161200&key=AIzaSyAtpji5Vk271Qu6_QFSBXwK7wpoCQLY-zQ");
$timez = json_decode($google_time);
$d = new DateTime("now", new DateTimeZone($timez->timeZoneId));
return  $d->format('H : i: s');
}

echo  get_timee("Peru","Lima");
?>

使用此功能可节省您的时间。

相关问题