How to find timezone ID from IP address/country name in PHP?

后端 未结 4 491
北荒
北荒 2020-12-02 01:40

Can anyone please tell me if there is any way available in PHP to fetch timezone region like(\'Asia/Calcutta\') from IP Address or country name?

Description:

<
相关标签:
4条回答
  • 2020-12-02 02:19

    If you want to do it on the server side, you can do this with PHP:

    $url = "https://ipsidekick.com/json";
    $result = file_get_contents($url);
    $json = json_decode($result, true);
    print("It's " . $json["gmtOffset"] . " at " . $json["timeZoneName"]);
    

    Note that this is synchronous.

    Alternatively, if you can do it on the client side. You can use this with jQuery or equivalent:

    $.get("https://ipsidekick.com/json", function(response) {
      console.log(response.ip +
        " It's " + response.gmtOffset +
        " from " + response.timeZoneName);
    });
    

    Disclaimer: I run IP Sidekick.

    0 讨论(0)
  • 2020-12-02 02:26

    This way you get time zone..

    <?php
    $jsondata = file_get_contents("https://ipapi.co/json");
    $data = json_decode($jsondata, true);
    echo '<pre>';
    print_r($data);
    echo '</pre>';
    
    //Timezone
    echo $data['timezone'];
    ?>
    
    //OUTPUT
    Array
    (
        [ip] => 2405:204:f089:d0e8:b1e7:a83e:9e2d:1bb5
        [city] => Bhubaneswar
        [region] => Odisha
        [region_code] => OR
        [country] => IN
        [country_name] => India
        [continent_code] => AS
        [postal] => 
        [latitude] => 20.2333
        [longitude] => 85.8333
        [timezone] => Asia/Kolkata
        [utc_offset] => +0530
        [country_calling_code] => +91
        [currency] => INR
        [languages] => en-IN,hi,bn,te,mr,ta,ur,gu,kn,ml,or,pa,as,bh,sat,ks,ne,sd,kok,doi,mni,sit,sa,fr,lus,inc
        [asn] => AS55836
        [org] => Reliance Jio Infocomm Limited
    )
    
    Asia/Kolkata
    
    0 讨论(0)
  • 2020-12-02 02:27

    I consider a better idea is to finding out which timezone is set based on the user client setting. Maybe there is the situation that a user is in a timezone, which he doesn't want to be.

    One simple possibility is to ask the user, but that's to easy.

    With JavaScript you could find out the offset in minutes from UTC. Also you could find out, if the client is in a country with daylight saving time (DST) and if dst is active. This could be done, by comparing the time of two date objects, one with the month January and one with the month July.

    var dst1, dst2, expires, hemisphere, now;
    
    now = new Date(); expires = new Date(); dst1 = new Date(); dst2 = new Date();
    
    expires.setTime(now.getTime() + 31536000000);
    setCookie('timezone_offset', now.getTimezoneOffset(), expires, '/');
    
    dst1.setDate(1);
    dst1.setMonth(1);
    dst2.setDate(1);
    dst2.setMonth(7);
    
    if (parseInt(dst1.getTimezoneOffset()) === parseInt(dst2.getTimeZoneOffset())) {
      setCookie('timezone_dst', 0, expires, '/');
    } else {
      hemisphere = parseInt(d1.getTimezoneOffset()) - parseInt(d2.getTimezoneOffset());
      if ((hemisphere > 0 && parseInt(d1.getTimezoneOffset()) === parseInt(now.getTimezoneOffset())) || (hemisphere < 0 && parseInt(d2.getTimezoneOffset()) === parseInt(now.getTimezoneOffset()))) {
        setCookie('timezone_dst', '0', expires, '/');
      } else {
        setCookie('timezone_dst', '1', expires, '/');
      }
    }
    

    Read the cookie with PHP and interpreting the information of them with timezone_name_from_abbr():

    $timezone = timezone_name_from_abbr('', $_COOKIE['timezone_offset'] * 60, $_COOKIE['timezone_dst']);
    
    0 讨论(0)
  • 2020-12-02 02:46

    Using IP Address

    Timezones follow sometimes quirky rules. IP geolocation is anything but precise (notwithstanding the claims of some vendors).

    Having said that, you can certainly find the city and country using a Geo IP product such as MaxMind's:

    http://www.maxmind.com/app/geolite

    which includes a PHP module

    http://www.maxmind.com/app/php

    You can then use the MaxMind APIs to try and estimate the user's timezone

    http://www.maxmind.com/app/faq#timezone

    Alternative

    If you want to rely on your user's clock rather than the IP address, jsTimezoneDetect works quite well (though it is not perfect).

    https://bitbucket.org/pellepim/jstimezonedetect/wiki/Home

    Summary

    Neither technique works perfectly in all cases. Be sure you allow your user to correct any auto-generated timezone suggestion.

    0 讨论(0)
提交回复
热议问题