// https://inkplant.com/code/get-timezone
<?php
function get_timezone($latitude,$longitude,$username) {
//error checking
if (!is_numeric($latitude)) { custom_die('A numeric latitude is required.'); }
if (!is_numeric($longitude)) { custom_die('A numeric longitude is required.'); }
if (!$username) { custom_die('A GeoNames user account is required. You can get one here: http://www.geonames.org/login'); }
//connect to web service
$url = 'http://ws.geonames.org/timezone?lat='.$latitude.'&lng='.$longitude.'&style=full&username='.urlencode($username);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);
if (!$xml) { $GLOBALS['error'] = 'The GeoNames service did not return any data: '.$url; return false; }
//parse XML response
$data = new SimpleXMLElement($xml);
//echo '<pre>'.print_r($data,true).'</pre>'; die();
$timezone = trim(strip_tags($data->timezone->timezoneId));
if ($timezone) { return $timezone; }
else { $GLOBALS['error'] = 'The GeoNames service did not return a time zone: '.$url; return false; }
}
?>