我有一个格式化的地址,例如Bhubaneswar,Odisha,India。如何在php代码中获取此地点的时区,即“Asia/Calcutta”?
我可以使用googlemapapi来获取这个,首先从该地址获取纬度和经度,然后使用这些纬度和经度以及Google时区API密钥,在javascript中获取时区。
我想在纯php代码中获取时区。我能做到吗?
感谢Avd
我有一个格式化的地址,例如Bhubaneswar,Odisha,India。如何在php代码中获取此地点的时区,即“Asia/Calcutta”?
我可以使用googlemapapi来获取这个,首先从该地址获取纬度和经度,然后使用这些纬度和经度以及Google时区API密钥,在javascript中获取时区。
我想在纯php代码中获取时区。我能做到吗?
感谢Avd
file_get_contents()
尝试一下:<?php
$url = "https://maps.googleapis.com/maps/api/timezone/json?location=20,85×tamp=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" }
将时间戳替换为当前时间戳或您想要的日期/时间对应的时间戳。
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}×tamp=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;
}
// 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>";
}
使用PHP和JS获取用户时区(无需API)(2020年更新)
检测用户时区是一个两步骤的过程:
使用Javascript获取浏览器的时区偏移量,并将其发送到PHP(通过AJAX或其他方式)。
将时区偏移量转换为有效的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