如何在PHP中获取一个地方的时区

时间:2014-02-28 07:12:55

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

我有一个像Bhubaneswar,Odisha,India这样的地方的格式化地址。如何在PHP代码中获得这个地方的时区,即“Asia / Calcutta”?

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

我希望时区纯粹是php代码。我可以吗?

感谢Avd

4 个答案:

答案 0 :(得分:3)

当然可以。试试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" }

将时间戳替换为当前时间戳或与所需日期/时间对应的时间戳。

答案 1 :(得分:0)

如果您找到此主题,请查看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

答案 2 :(得分:0)

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

检测用户时区是一个两步过程:

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

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

1)获取浏览器时区偏移

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

应该注意,getTimezoneOffset返回的偏移量是0,如果本地时区在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

答案 3 :(得分:-1)

 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;

}

请使用此功能