Last active
March 30, 2017 17:25
-
-
Save Lemmings19/b027010c56bfb562ffea38b13ed418e5 to your computer and use it in GitHub Desktop.
Get timezones for a bunch of latitudes and longitudes
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 | |
/** | |
* Using Google's Google Maps Time Zone API, get the timezones for a bunch of locations which have latitudes and longitudes. | |
* Output the timezones to a file. | |
* | |
* Note that this WILL NOT APPEND if the file already exists. It WILL OVERWRITE. | |
* | |
* The API returns the following output: | |
* | |
* { | |
* "dstOffset" : 3600, | |
* "rawOffset" : -28800, | |
* "status" : "OK", | |
* "timeZoneId" : "America/Vancouver", | |
* "timeZoneName" : "Pacific Daylight Time" | |
* } | |
* | |
* In this gist, we are only using the timeZoneId. | |
* | |
* Access the API and get your key here: | |
* https://console.developers.google.com/apis/api/timezone-backend.googleapis.com/overview | |
* | |
*/ | |
// Populate this with your list of latitudes and longitudes: | |
// I am using the key as an ID. | |
$places = [ | |
1 => ['lat' => 14.000000, 'lng' => -14.000000], | |
2 => ['lat' => 50.000000, 'lng' => -114.000000], | |
// as many as you like... | |
]; | |
// Google Maps Time Zone API key: | |
$key = 'abc123'; | |
// The file you want the results saved to: | |
$file = fopen("/somewhere/yourfile.txt", "w"); | |
// If we want to rate limit by count: | |
// 0 for unlimited. The Google enforced hard limit for a free account is 2500. | |
$limit = 0; | |
// Just keeping track of how many we've processed: | |
$count = 0; | |
// If we want to rate limit by time: | |
$timeBetweenCurls = 0; | |
$curl = curl_init(); | |
foreach ($places as $id => $place) { | |
$url = "https://maps.googleapis.com/maps/api/timezone/json?location=" . $place['lat'] . "," . $place['lng'] . "×tamp=" . time() . "&key=" . $key; | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
$response = json_decode(curl_exec($curl), true); | |
// Customize this to whatever format you want: | |
$output = "\nTimezone is " . $response['timeZoneId'] . " for " . $id . "."; | |
// If we want to see the results as we're getting them: | |
// print($output); | |
fwrite($file, $output); | |
$count++; | |
if ($limit > 0 && $count >= $limit) { | |
break; | |
} else { | |
sleep($timeBetweenCurls); | |
} | |
} | |
curl_close($curl); | |
fclose($file); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment