CakeFest 2024: The Official CakePHP Conference

DateTimeZone::getOffset

timezone_offset_get

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

DateTimeZone::getOffset -- timezone_offset_getGMT からのタイムゾーンのオフセットを返す

説明

オブジェクト指向型

public DateTimeZone::getOffset(DateTimeInterface $datetime): int

手続き型

この関数は、datetime パラメータで指定した 日付/時刻 についての GMT へのオフセットを返します。GMT オフセットの計算の際には、 使用する DateTimeZone オブジェクトに含まれるタイムゾーン情報を使用します。

パラメータ

object

手続き型のみ: timezone_open() が返す DateTimeZone オブジェクト

datetime

オフセットを計算する 日付/時刻 を含む DateTime。

戻り値

成功した場合にタイムゾーンのオフセット秒数を返します。

例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);
?>

add a note

User Contributed Notes 2 notes

up
-2
Anonymous
1 year ago
int offset does not cover fractional offsets of "rogue" locations such as Nepal etc.
up
-3
Daniel Vidal
1 year ago
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
*/

?>
To Top