Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lusareal/f7ec56c3d01f3801fe0dbd5257d48974 to your computer and use it in GitHub Desktop.
Save lusareal/f7ec56c3d01f3801fe0dbd5257d48974 to your computer and use it in GitHub Desktop.
Gets the Timezone from an Address based on the current time
<?php
$addr = 'Columbus,OH,43235';
$maps_api_key = 'insert key here';
/**
* Gets the timezone id (eg. America/New_York, America/Detroit)
* @param string $address
* @return string|bool
*/
function get_timezone_from_address($address){
$geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.trim(preg_replace(' ', '+', $address)).'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$lng = $output->results[0]->geometry->location->lng;
$timezone = file_get_contents('https://maps.googleapis.com/maps/api/timezone/json?key='.$maps_api_key.'&location='.$lat.','.$lng.'&timestamp='.time());
$tz_result = json_decode($timezone);
return isset($tz_result->timeZoneId) ? $tz_result->timeZoneId : false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment