int offset does not cover fractional offsets of "rogue" locations such as Nepal etc.
(PHP 5 >= 5.2.0, PHP 7, PHP 8)
DateTimeZone::getOffset -- timezone_offset_get — GMT からのタイムゾーンのオフセットを返す
オブジェクト指向型
手続き型
この関数は、datetime
パラメータで指定した 日付/時刻
についての GMT へのオフセットを返します。GMT オフセットの計算の際には、
使用する DateTimeZone オブジェクトに含まれるタイムゾーン情報を使用します。
成功した場合にタイムゾーンのオフセット秒数を返します。
例1 DateTimeZone::getOffset() の例
<?php
// ふたつのタイムゾーンオブジェクトを作成します。ひとつは台北 (台湾)、
// そしてもうひとつは東京 (日本) のものです。
$dateTimeZoneTaipei = new DateTimeZone("Asia/Taipei");
$dateTimeZoneJapan = new DateTimeZone("Asia/Tokyo");
// 同一の Unix タイムスタンプを持つふたつの DateTime オブジェクトを作成します。
// しかしアタッチするタイムゾーンはそれぞれ異なります。
$dateTimeTaipei = new DateTime("now", $dateTimeZoneTaipei);
$dateTimeJapan = new DateTime("now", $dateTimeZoneJapan);
// $dateTimeTaipei オブジェクトに含まれる 日付/時刻 の GMT オフセットを計算します。
// しかし、タイムゾーンの規則は東京のもの ($dateTimeZoneJapan)
// を使用します。
$timeOffset = $dateTimeZoneJapan->getOffset($dateTimeTaipei);
// これは int(32400) となります (Sat Sep 8 01:00:00 1951 JST 以降の日付の場合)。
var_dump($timeOffset);
?>
int offset does not cover fractional offsets of "rogue" locations such as Nepal etc.
Note that the DateTime parameter has no effect in the result returned by DateTimeZone::getOffset($DateTime), unless, it refers to a DateTime where there is daylight savings in the referenced DateTimeZone.Ex.:<?php$timezone_brl = new DateTimeZone('America/Sao_Paulo');$timezone_eng = new DateTimeZone('Europe/London');$timezone_aus = new DateTimeZone('Australia/Brisbane');$dateTimes = [ new DateTime() , new DateTime('now', $timezone_eng) , new DateTime('now', $timezone_aus) , new DateTime('now', $timezone_brl) , new DateTime('2000-06-10', $timezone_brl) , new DateTime('2000-12-10', $timezone_brl) , new DateTime('2020-12-10', $timezone_brl)];foreach($dateTimes as $dateTime){ echo "\n" . $timezone_brl->getOffset($dateTime);}/*** -10800* -10800* -10800* -10800* -10800 // No daylight savings in June/2000* - 7200 // Brazil had daylight savings until 2020* -10800 // No more daylight savings, so it returns -10800*/?>