簡體   English   中英

如何在php中獲取某個地方的時區

[英]How to get Time Zone of a place in php

我有一個像印度奧里薩邦布巴內斯瓦爾這樣的地方的格式化地址。我怎樣才能在 php 代碼中獲得這個地方的時區,即“亞洲/加爾各答”?

我可以使用 googlemapapi 獲取這個,即首先我從這個地址獲取緯度和經度,並使用這個緯度和經度以及谷歌時區 api 鍵我在 javascript 中獲取時區。

我想要純 php 代碼中的時區。 我可以嗎 ?

感謝 Avd

你當然可以。 嘗試使用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" }

將時間戳替換為當前時間戳或與您想要的日期/時間對應的時間戳。

 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;

}

請使用此功能

使用 PHP 和 JS 獲取用戶時區(無 API)(2020 年更新)

檢測用戶時區是一個兩步過程:

  1. 使用Javascript獲取瀏覽器的時區偏移量,並將其發送到PHP(通過AJAX或其他方式)。

  2. 將時區偏移量轉換為有效的 TZ 數據庫時區名稱,例如 America/New_York,Asia/Kolkata。

1) 獲取瀏覽器時區偏移量

Javascript有一個getTimezoneOffset方法,它給出從當前本地時間到 UTC 的時區差異,以分鍾為單位。 我們需要將此偏移量傳遞給服務器。

應該注意的是,如果本地時區落后於 UTC,則getTimezoneOffset返回一個偏移量,如果本地時區領先,則返回負值。 所以我們必須在偏移量上添加一個相反的符號(+ 或 -)。

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函數從以分鍾為單位的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

如果您找到此線程,請查看 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM