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

后端 未结 4 514
北荒
北荒 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.

提交回复
热议问题