使用国家/地区名称或城市名称在国家/地区获取时间(如果国家/地区有多个时区,可以获得更准确的时间)
例如
echo get_local_time('England'); //8:23pm
echo get_local_time('New York'); //7:23am
Run Code Online (Sandbox Code Playgroud)
这可能纯粹用PHP /那里有任何API吗?
小智 9
这是使用谷歌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×tamp=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");
?>
Run Code Online (Sandbox Code Playgroud)
使用此功能可节省您的时间.
| 归档时间: |
|
| 查看次数: |
2755 次 |
| 最近记录: |