2

What's the point of PHP's DateTimeZone::getOffset's (timezone_offset_get) datetime parameter?

<?php

$tz = new DateTimeZone("Asia/Tokyo");

var_dump($tz->getOffset(new DateTime())); // 32400
var_dump($tz->getOffset(new DateTime('now', new DateTimeZone('Asia/Taipei')))); // 32400
var_dump($tz->getOffset(new DateTime('now', new DateTimeZone('Europe/Budapest')))); // 32400
var_dump($tz->getOffset(new DateTime('2000-01-01', new DateTimeZone('America/New_York')))); // 32400

Am I missing something, or the parameter doesn't affect the output of the function at all?

The documentation mentions the function computes the offset from/for the parameter. What does this mean?

1 Answer 1

3

Because the offset depends on the date.

For example, in summer Europe/Budapest has an offset of 2 hours (because of daylight saving time), in winter of 1 hour:

$tz = new DateTimeZone("Europe/Budapest");

var_dump($tz->getOffset(new DateTime('2018-06-06', $tz))); // 7200
var_dump($tz->getOffset(new DateTime('2018-01-06', $tz))); // 3600

Daylight saving time is not the only source of change, see for example Moscow time:

$tz = new DateTimeZone("Europe/Moscow");

var_dump($tz->getOffset(new DateTime('2010-01-01', $tz))); // 10800
var_dump($tz->getOffset(new DateTime('2012-01-01', $tz))); // 14400
var_dump($tz->getOffset(new DateTime('2015-01-01', $tz))); // 10800
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.