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

3

我有一个格式化的地址,例如Bhubaneswar,Odisha,India。如何在php代码中获取此地点的时区,即“Asia/Calcutta”?

我可以使用googlemapapi来获取这个,首先从该地址获取纬度和经度,然后使用这些纬度和经度以及Google时区API密钥,在javascript中获取时区。

我想在纯php代码中获取时区。我能做到吗?

感谢Avd


你尝试过将国家名称映射到时区吗? - anurupr
你想将默认时间设置为亚洲/加尔各答吗? - Prafulla
尝试使用像Yahoo地点查找器这样的服务。 - Nouphal.M
你的问题不够清晰。一个地方的时区是不会改变的,所以如果你想知道加尔各答的时区,它是UTC/GMT +5:30小时。也许你想进行某种计算?或者你想从地址中检测城市并展示/存储时区?你能澄清一下你想要什么吗? - PatomaS
1
你们刚才读了这个问题吗? - MrUpsidown
显示剩余2条评论
4个回答

5
当然可以。使用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
 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;

}

请使用这个函数。

1
这个问题明确要求一个“纯粹的PHP代码”解决方案。这段代码仍然使用了几个外部API;你只是用GeoNames API替换了Google Maps时区API。(使用“演示”帐户,不应在生产代码中使用…) - user149341

-1
如果您找到了这个线程,请查看timezoneapi.io。
使用地址API,您可以请求地址。请求返回:
  • 地址
  • 时区(以及有关时区的大量相关信息)
  • 有关时区的日期/时间,例如时区中的时间是什么,时区是否处于DST状态,货币,首都等。
在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


-1

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

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

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

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

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

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

应注意,getTimezoneOffset返回的偏移量如果本地时区落后于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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接