Skip to content

Instantly share code, notes, and snippets.

@mycarrysun
Created December 27, 2017 20:11
Show Gist options
  • Save mycarrysun/e4dfe947dc8ce7421bb2fd81eeed0093 to your computer and use it in GitHub Desktop.
Save mycarrysun/e4dfe947dc8ce7421bb2fd81eeed0093 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