【问题标题】:How to get Time Zone of a place in php如何在php中获取一个地方的时区
【发布时间】:2014-04-01 01:39:20
【问题描述】:

我有一个像布巴内斯瓦尔、奥里萨邦、印度这样的地方的格式化地址。如何在 php 代码中获取这个地方的时区,即“亚洲/加尔各答”?

我可以使用 googlemapapi 获取此信息,即首先我从该地址获取纬度和经度,并使用此纬度和经度以及谷歌时区 api 密钥获取 javascript 中的时区。

我想要纯 php 代码中的时区。可以吗?

感谢 Avd

【问题讨论】:

  • 您是否尝试过将国家/地区名称映射到时区?
  • 您想将默认时间设置为亚洲/加尔各答吗??
  • 尝试使用 Yahoo placefinder 等服务。
  • 你的问题不清楚。一个地方的时区不会改变,所以如果你想要加尔各答的时区,它是 UTC/GMT +5:30 小时。也许你想做某种计算?还是您想从地址中检测城市并同时显示/存储时区?你能澄清一下你想要什么吗?
  • 你们刚刚阅读问题了吗?

标签: php google-maps-api-3 timezone


【解决方案1】:

使用 PHP 和 JS(无 API)获取用户时区(2020 年更新)

检测用户时区分为两步:

  1. 使用 Javascript 获取浏览器的时区偏移量,并将其发送到 PHP(通过 AJAX 或其他方式)。

  2. 将时区偏移量转换为有效的 TZ 数据库时区名称,例如 America/New_York,Asia/Kolkata。

1) 获取浏览器时区偏移量

Javascript 有一个 getTimezoneOffset 方法,它给出了从当前本地时间到 UTC 的时区差异,以分钟为单位。我们需要将此偏移量传递给服务器。

需要注意的是,getTimezoneOffset 返回一个偏移量,如果本地时区落后于 UTC,则该偏移量为正值,如果超过 UTC,则该偏移量为负值。所以我们必须在偏移量上添加一个相反的符号(+ 或 -)。

var timezone_offset_minutes = new Date().getTimezoneOffset();
timezone_offset_minutes = timezone_offset_minutes == 0 ? 0 : -timezone_offset_minutes;

// Timezone difference in minutes such as 330 or -360 or 0
console.log(timezone_offset_minutes); 

2) 在 PHP 中,以分钟为单位的时区偏移量转换为时区名称

您可以通过timezone_name_from_abbr函数从时区偏移量中获取时区名称。

// This is just an example. In application this will come from Javascript (via an AJAX or something)
$timezone_offset_minutes = 330;  // $_GET['timezone_offset_minutes']

// Convert minutes to seconds
$timezone_name = timezone_name_from_abbr("", $timezone_offset_minutes*60, false);

// Asia/Kolkata
echo $timezone_name;

现在,当您获取时区名称时,您可以获取相对于用户时区的日期和时间:

date_default_timezone_set($timezone_name);

来源:https://usefulangle.com/post/31/detect-user-browser-timezone-name-in-php

【讨论】:

    【解决方案2】:

    如果您找到此线程,请查看 timezoneapi.io。

    使用地址 API,您可以请求地址。请求返回:

    • 地址
    • 时区(以及很多关于时区的相关信息)
    • 时区的日期/时间,例如时区是几点,是夏令时的时区,货币,首都等。

    在 PHP 中

    // Get JSON object
    $jsondata = file_get_contents("https://timezoneapi.io/api/address/?Hacker+Way+1+Menlo+Park+California");
    
    // Decode
    $data = json_decode($jsondata, true);
    
    // Request OK?
    if($data['meta']['code'] == '200'){
    
        // Example: Get the city parameter
        echo "City: " . $data['data']['addresses']['0']['city'] . "<br>";
    
        // Example: Get the users time
        echo "Time: " . $data['data']['addresses']['0']['datetime']['date_time_txt'] . "<br>";
    
    }
    

    更多文档在这里: https://timezoneapi.io/developers/address

    【讨论】:

      【解决方案3】:
       public function getTimezone($location)
      {
          $location = urlencode($location);
          $APIkey = "PLACE API KEY HERE";
          if(!$APIkey){ return false;}
          $url = "https://maps.googleapis.com/maps/api/geocode/json?address={$location}&key=$APIkey";
          $data = file_get_contents($url);
          // Get the lat/lng out of the data
          $data = json_decode($data);
          if(!$data) return false;
          if(!is_array($data->results)) return false;
          if(!isset($data->results[0])) return false;
          if(!is_object($data->results[0])) return false;
          if(!is_object($data->results[0]->geometry)) return false;
          if(!is_object($data->results[0]->geometry->location)) return false;
          if(!is_numeric($data->results[0]->geometry->location->lat)) return false;
          if(!is_numeric($data->results[0]->geometry->location->lng)) return false;
          $lat = $data->results[0]->geometry->location->lat;
          $lng = $data->results[0]->geometry->location->lng;
      
          // get the API response for the timezone
          //$timezoneAPI = "https://maps.googleapis.com/maps/api/timezone/json?location={$lat},{$lng}&timestamp=1331161200&key=AIzaSyBtEwgQX2k0fijo3EsFlOLgXxEvkDpDyeI";
      
          $timezoneAPI = "http://api.geonames.org/timezoneJSON?lat={$lat}&lng={$lng}&username=demo";
      
          $response = file_get_contents($timezoneAPI);
          if(!$response)
              return false;
      
          $response = json_decode($response);
      
          if(!is_object($response))
              return false;
      
          if(isset($response->timezoneId) && !is_string($response->timezoneId)) // If google api, use timeZoneId
              return false;
      
          return $response->timezoneId;
      
      }
      

      请使用此功能

      【讨论】:

      • 该问题专门要求提供“纯 php 代码”的解决方案。这段代码仍然使用了几个外部 API;您刚刚将 GeoNames API 替换为 Google Maps 时区 API。 (使用“演示”帐户,不应在生产代码中使用......)
      【解决方案4】:

      当然可以。试试file_get_contents():

      <?php
      $url = "https://maps.googleapis.com/maps/api/timezone/json?location=20,85&timestamp=1393575206&sensor=false";
      $json_timezone = file_get_contents($url);
      print_r($json_timezone);
      ?>
      

      这将打印:

      { "dstOffset" : 0, "rawOffset" : 19800, "status" : "OK", "timeZoneId" : "Asia/Calcutta", "timeZoneName" : "India Standard Time" }
      

      将时间戳替换为当前时间戳或您想要的日期/时间对应的时间戳。

      【讨论】:

        猜你喜欢
        • 2015-02-09
        • 2012-01-28
        • 1970-01-01
        • 1970-01-01
        • 2010-10-01
        • 2020-07-23
        • 2020-10-26
        • 2015-06-12
        • 2013-01-13
        相关资源
        最近更新 更多