从纬度和经度获取 PHP 时区名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3126878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:45:45  来源:igfitidea点击:

Get PHP Timezone Name from Latitude and Longitude?

phpgeolocationtimezonelatitude-longitude

提问by Navarr

Is there a way get the timezone of a user by their latitude and longitude? And not just the offset, but the actual timezone they're in.

有没有办法通过纬度和经度获取用户的时区?不仅是偏移量,还有它们所在的实际时区。

Essentially, I'm searching for the polar opposite of DateTimeZone::getLocation which returns the latitude and longitude for a certain timezone.

从本质上讲,我正在寻找 DateTimeZone::getLocation 的极端对立面,它返回某个时区的纬度和经度。

采纳答案by thomasfedb

Geonames should do the job nicely:

Geonames 应该很好地完成这项工作:

http://www.geonames.org/

http://www.geonames.org/

They've also got a php library.

他们还有一个 php 库。

回答by j-bin

For those who wants to get timezone from country code, latitude and longitude. ( easy to get it if you have a geoip module installed on your server )

对于那些想要从国家代码、纬度和经度获取时区的人。(如果您的服务器上安装了 geoip 模块,则很容易获得它)

Try this, I've added a distance calculation - only for those countries which has multiple timezones. Ah, and the country code is a two letter ISO code.

试试这个,我添加了一个距离计算 - 仅适用于具有多个时区的国家。啊,国家代码是两个字母的 ISO 代码。

// ben@jp

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';
}
//timezone for one NY co-ordinate
echo get_nearest_timezone(40.772222,-74.164581) ;
// more faster and accurate if you can pass the country code 
echo get_nearest_timezone(40.772222, -74.164581, 'US') ;

回答by dav

A good resource is the Google Time Zone API.

一个很好的资源是 Google Time Zone API。

Documentation: https://developers.google.com/maps/documentation/timezone/

文档https: //developers.google.com/maps/documentation/timezone/

It takes latitudeand longitudeand returns array like this:

它接受latitudelongitude返回这样的数组:

array(
    'dstOffset' => (int) 3600,
    'rawOffset' => (int) -18000,
    'status' => 'OK',
    'timeZoneId' => 'America/New_York',
    'timeZoneName' => 'Eastern Daylight Time'
)

...but there are some limits:

...但有一些限制:

[updated 2019]The Google Time Zone API has usage limits in place. Basically, billing must be enabled on your project, but a $200 USD "Google Maps Platform credit" is applied each month (so in most cases your first 40,000 Time Zone API calls/month would be free of charge).

[2019 年更新]Google 时区 API 有使用限制。基本上,必须对您的项目启用计费,但每月应用 200 美元的“Google Maps Platform 信用”(因此在大多数情况下,您每月前 40,000 次时区 API 调用是免费的)。

回答by Uddhav Kambli

I did a timezone solution recently in an 8 hour long hackathon. It's quickly put together and I'd love to develop it further and sell it as a product but since there is no way for me to do it, I've open sourced it at my github.

我最近在 8 小时的黑客马拉松中做了一个时区解决方案。它很快就组装好了,我很想进一步开发它并将其作为产品出售,但由于我没有办法做到这一点,我已经在我的 github 上开源了它。

There is a demotoo but it may go down if it hits resource limits. It's a free webapp on Google App Engine.

也有一个演示,但如果达到资源限制,它可能会关闭。它是 Google App Engine 上的免费网络应用程序。

You can definitely optimize/augment this further in wrt - running time, space, data - to suit your needs.

您绝对可以在 wrt 中进一步优化/增强它 - 运行时间、空间、数据 - 以满足您的需求。

回答by Jiho Kang

The Yahoo places API provides timezone information via reverse geolocation.

雅虎地点 API 通过反向地理定位提供时区信息。

Check it out.

一探究竟。

http://developer.yahoo.com/geo/placefinder/guide/requests.html

http://developer.yahoo.com/geo/placefinder/guide/requests.html

回答by Dziamid

How about finding the closest point to the one in the list of all timezone locations? I wonder how accurate is this?

如何在所有时区位置列表中找到最接近的点?我想知道这有多准确?

UPDATE: Eventually, I came up with this snippet that works for me. This will work fine for all locations, but may not be accurate for those close to borders.

更新:最终,我想出了这个对我有用的片段。这适用于所有位置,但对于靠近边界的位置可能不准确。

  /**
   * Attempts to find the closest timezone by coordinates
   *
   * @static
   * @param $lat
   * @param $lng
   */
  public static function getClosestTimezone($lat, $lng)
  {
    $diffs = array();
    foreach(DateTimeZone::listIdentifiers() as $timezoneID) {
      $timezone = new DateTimeZone($timezoneID);
      $location = $timezone->getLocation();
      $tLat = $location['latitude'];
      $tLng = $location['longitude'];
      $diffLat = abs($lat - $tLat);
      $diffLng = abs($lng - $tLng);
      $diff = $diffLat + $diffLng;
      $diffs[$timezoneID] = $diff;

    }

    //asort($diffs);
    $timezone = array_keys($diffs, min($diffs));


    return $timezone[0];

  }