簡體   English   中英

要偏移的 PHP 時區名稱

[英]PHP Timezone Name to Offset

我將如何將時區名稱(“亞洲/迪拜”、“歐洲/安道爾”等)轉換為幾小時(甚至幾秒,因為我可以從那里轉換)的 GMT 偏移量。

大多數人要求相反,但我需要從 GeoNames 提供的時區名稱中獲取時區偏移量。

你可以使用以下

<?php

$timezone = 'Pacific/Nauru';
$time = new \DateTime('now', new DateTimeZone($timezone));
$timezoneOffset = $time->format('P');
?>

另一種選擇是使用可用的DateTimeZone::getOffset()方法,該方法返回指定日期/時間時區的偏移量(以秒為單位)。

$timezone = new DateTimeZone("Europe/Andorra");
$offset   = $timezone->getOffset(new DateTime);

我喜歡使用PHP Carbon

$offset = Carbon::now('Europe/Andorra')->offsetHours;

暫無
暫無

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

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