Get PHP Timezone Name from Latitude and Longitude?

2019-02-03 04:03发布

This question already has an answer here:

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.

6条回答
唯我独甜
2楼-- · 2019-02-03 04:38

The Yahoo places API provides timezone information via reverse geolocation.

Check it out.

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

查看更多
在下西门庆
3楼-- · 2019-02-03 04:45

What about google time zone api

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

it takes latitude and longitude and returns array like this

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

but there are some limits

The Google Time Zone API has the following limits in place:

Users of the free API:
2,500 requests per 24 hour period.
10 requests per second.

Maps for Business customers:
100,000 requests per 24 hour period.
10 requests per second.

查看更多
姐就是有狂的资本
4楼-- · 2019-02-03 04:47

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 )

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.

// 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') ;
查看更多
干净又极端
5楼-- · 2019-02-03 04:50

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.

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

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

查看更多
放荡不羁爱自由
6楼-- · 2019-02-03 04:50

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];

  }
查看更多
爷、活的狠高调
7楼-- · 2019-02-03 04:52

Geonames should do the job nicely:

http://www.geonames.org/

They've also got a php library.

查看更多
登录 后发表回答