In case anyone stumbles upon this thread, here's another solution. At timezoneapi.io you can request an IP address and get several objects in return (I've created the service). It was created because I needed to know which timezone my users were in, where in the world and what time it currently is.
In PHP - returns location, timezone and date/time:
// Get IP address
$ip_address = getenv('HTTP_CLIENT_IP') ?: getenv('HTTP_X_FORWARDED_FOR') ?: getenv('HTTP_X_FORWARDED') ?: getenv('HTTP_FORWARDED_FOR') ?: getenv('HTTP_FORWARDED') ?: getenv('REMOTE_ADDR');
// Get JSON object
$jsondata = file_get_contents("http://timezoneapi.io/api/ip/?" . $ip_address);
// Decode
$data = json_decode($jsondata, true);
// Request OK?
if($data['meta']['code'] == '200'){
// Example: Get the city parameter
echo "City: " . $data['data']['city'] . "
";
// Example: Get the users time
echo "Time: " . $data['data']['datetime']['date_time_txt'] . "
";
}
Using jQuery:
// Get JSON object
$.getJSON('https://timezoneapi.io/api/ip', function(data){
// Request OK?
if(data.meta.code == '200'){
// Log
console.log(data);
// Example: Get the city parameter
var city = data.data.city;
alert(city);
// Example: Get the users time
var time = data.data.datetime.date_time_txt;
alert(time);
}
});