使用国名或城市名获得国家时间(如果可用)(以获得更准确的时间,如果在全国多个时区) [英] Get time in country using country name or city name if available (to get a more exact time if multiple timezones in country)

查看:271
本文介绍了使用国名或城市名获得国家时间(如果可用)(以获得更准确的时间,如果在全国多个时区)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Get time in country using country name or city name if available (to get a more exact time if multiple timezones in country)

例如

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

这可能与纯粹的PHP /是否有任何的API了吗?

Is this possible purely with PHP/ are there any APIs out there?

推荐答案

这是使用谷歌API和PHP来获得国家名称和城市名称的时间code。

This is the code using google api and php to get the time from country name and city name.

<?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");
?>

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

Use this function and save your time.

这篇关于使用国名或城市名获得国家时间(如果可用)(以获得更准确的时间,如果在全国多个时区)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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