Skip to content

Instantly share code, notes, and snippets.

@Casperhr
Created August 1, 2014 07:18
Show Gist options
  • Select an option

  • Save Casperhr/596219f4421955ff740f to your computer and use it in GitHub Desktop.

Select an option

Save Casperhr/596219f4421955ff740f to your computer and use it in GitHub Desktop.
public static function getClosestTimezone($lat, $lng) {
$diffs = array();
foreach(DateTimeZone::listIdentifiers() as $timezoneID) {
$timezone = new DateTimeZone($timezoneID);
$location = $timezone->getLocation();
$tLat = $location['latitude'];
$tLng = $location['longitude'];
$diffLat = abs($lat - $tLat);
$diffLng = abs($lng - $tLng);
$diff = $diffLat + $diffLng;
$diffs[$timezoneID] = $diff;
}
$timezone = array_keys($diffs, min($diffs));
return $timezone[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment