问题描述:

Is There any API or Plugin in PHP/Jquery, For getting local time by ip address?

I have only IP Address of all users those who are registered on my site, How do I get Local Time of that particular user by only IP Address?

网友答案:

This is worked for me

function get_localtime($country,$city)
{
    $country = str_replace(' ', '', $country);
    $city = str_replace(' ', '', $city);
    $geocode_stats = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=$city+$country,&sensor=false");
    $output_deals = json_decode($geocode_stats);
    $latLng = $output_deals->results[0]->geometry->location;
    $lat = $latLng->lat;
    $lng = $latLng->lng;    
    $google_time = file_get_contents("https://maps.googleapis.com/maps/api/timezone/json?location=$lat,$lng&timestamp=1331161200&key=AIzaSyAtpji5Vk271Qu6_QFSBXwK7wpoCQLY-zQ");
    $timez = json_decode($google_time);
    $d = new DateTime("now", new DateTimeZone($timez->timeZoneId));
    return  $d->format('H : i: s');
}

//------------------------------------------------------------------------------ Getting TimeZone by the Ip Address
$location = file_get_contents('http://api.ipinfodb.com/v3/ip-city/?key=xxxxxxxxxxx&ip=121.242.47.195&format=json');
//------------------------------------------------------------------------------

$location = json_decode($location,true);

if(is_array($location)){
    echo  get_timee("INDIA","MAHARASHTRA");
}else{
    echo '{"statusCode":"error"}';
}

Register for a free key here: http://ipinfodb.com/register.php (no limits but queued if more than 1 request per second)

相关阅读:
Top