I am trying to obtain client's time zone for one of my projects using PHP. Saw a post in stackoverflow with solutions for what I was looking for. Here's the link to that post.
I followed the most upvoted solution but it produced an error something like this.
Suppose $_SERVER['REMOTE_ADDR']
returns 212.88.207.202
to $ip
, I hardcoded that value to $ip
in my develpoment phase.
It doesn't matter since hardcoding 212.88.207.202
or using $_SERVER['REMOTE_ADDR']
this returns the same value to $ip
.
But when I run the page, I get this error.
Warning: file_get_contents(http://smart-ip.net/geoip-json/212.88.207.202): failed to open stream: No connection could be made because the target machine actively refused it.
Here's the code
<?php
$ip = '212.88.207.202'; // means we got user's IP address
$json = file_get_contents( 'http://smart-ip.net/geoip-json/' . $ip); // this one service we gonna use to obtain timezone by IP
// maybe it's good to add some checks (if/else you've got an answer and if json could be decoded, etc.)
$ipData = json_decode($json, true);
//var_dump($ipData);
if ($ipData['timezone']) {
$tz = new DateTimeZone( $ipData['timezone']);
$now = new DateTime( 'now', $tz); // DateTime object corellated to user's timezone
} else {
// we can't determine a timezone - do something else...
}
?>
Where did I go wrong???