1

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

For example

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

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

Jake
  • 3,326
  • 7
  • 39
  • 59
  • I don't understand your question. Are you asking PHP to determine the local time? See [supported timezones](http://php.net/manual/en/timezones.php). – Kermit Aug 30 '12 at 19:25
  • I need some kind of function that after inputing a country name or city name it returns the time there. – Jake Aug 30 '12 at 19:26
  • Possible duplicate in that case. See [this](http://stackoverflow.com/questions/1727077/generating-a-drop-down-list-of-timezones-with-php) or [this](http://stackoverflow.com/questions/4755704/php-timezone-list). – Kermit Aug 30 '12 at 19:29
  • http://www.geonames.org/export/web-services.html#timezone – salathe Aug 30 '12 at 19:32

1 Answers1

9

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.

user3767878
  • 189
  • 2
  • 5