【问题标题】:Get time in country using country name or city name if available (to get a more exact time if multiple timezones in country)使用国家名称或城市名称(如果可用)获取国家/地区的时间(如果国家/地区有多个时区,则获取更准确的时间)
【发布时间】:2012-08-25 14:28:15
【问题描述】:

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

例如

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

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

【问题讨论】:

标签: php api time


【解决方案1】:

这是使用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");
?>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-30
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多