• How to get Time Zone through IP Address in PHP

    Answered

    How to get Time Zone through IP Address in PHP

    Bronze Member Asked on May 26, 2017 in Php.
    Add Comment
  • 1 Answer(s)
    Best answer

     

    <?php
    $time_zone = getTimeZoneFromIpAddress();
    
    echo 'Your Time Zone is '.$time_zone;
    function getTimeZoneFromIpAddress(){
    
    $clientsIpAddress = get_client_ip();
    $clientInformation = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$clientsIpAddress));
    $clientsLatitude = $clientInformation['geoplugin_latitude'];
    
    $clientsLongitude = $clientInformation['geoplugin_longitude'];
    
    $clientsCountryCode = $clientInformation['geoplugin_countryCode'];
    $timeZone = get_nearest_timezone($clientsLatitude, $clientsLongitude, $clientsCountryCode) ;
    return $timeZone;
    }
    function get_client_ip() {
    
    $ipaddress = '';
    
    if (getenv('HTTP_CLIENT_IP'))
    
    $ipaddress = getenv('HTTP_CLIENT_IP');
    
    else if(getenv('HTTP_X_FORWARDED_FOR'))
    
    $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    
    else if(getenv('HTTP_X_FORWARDED'))
    
    $ipaddress = getenv('HTTP_X_FORWARDED');
    
    else if(getenv('HTTP_FORWARDED_FOR'))
    
    $ipaddress = getenv('HTTP_FORWARDED_FOR');
    
    else if(getenv('HTTP_FORWARDED'))
    
    $ipaddress = getenv('HTTP_FORWARDED');
    
    else if(getenv('REMOTE_ADDR'))
    
    $ipaddress = getenv('REMOTE_ADDR');
    
    else
    
    $ipaddress = 'UNKNOWN';
    
    return $ipaddress;
    
    }
    function get_nearest_timezone($cur_lat, $cur_long, $country_code = '') {
    
    $timezone_ids = ($country_code) ? DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $country_code)
    
    : DateTimeZone::listIdentifiers();
    if($timezone_ids && is_array($timezone_ids) && isset($timezone_ids[0])) {
    $time_zone = '';
    
    $tz_distance = 0;
    //only one identifier?
    
    if (count($timezone_ids) == 1) {
    
    $time_zone = $timezone_ids[0];
    
    } else {
    foreach($timezone_ids as $timezone_id) {
    
    $timezone = new DateTimeZone($timezone_id);
    
    $location = $timezone->getLocation();
    
    $tz_lat = $location['latitude'];
    
    $tz_long = $location['longitude'];
    $theta = $cur_long - $tz_long;
    
    $distance = (sin(deg2rad($cur_lat)) * sin(deg2rad($tz_lat)))
    
    + (cos(deg2rad($cur_lat)) * cos(deg2rad($tz_lat)) * cos(deg2rad($theta)));
    
    $distance = acos($distance);
    
    $distance = abs(rad2deg($distance));
    
    // echo '<br />'.$timezone_id.' '.$distance;
    if (!$time_zone || $tz_distance > $distance) {
    
    $time_zone = $timezone_id;
    
    $tz_distance = $distance;
    
    }
    }
    
    }
    
    return $time_zone;
    
    }
    
    return 'unknown';
    
    }
    
    

     

    Bronze Member Answered on May 26, 2017.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.