Forked from mycarrysun/PHP-get-timezone-from-address.php
Created
February 1, 2019 15:51
-
-
Save lusareal/f7ec56c3d01f3801fe0dbd5257d48974 to your computer and use it in GitHub Desktop.
Gets the Timezone from an Address based on the current time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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.'×tamp='.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