How does method timezone getOffset uses date on PHP? - php

I'm trying to understand how PHP uses the given datetime to return getOffset result on a DateTimeZone object. As far as I see, the result is always the same no matter what datetime I pass.
public function testGetOffset()
{
// UTC offset is 00:00 (0 seconds)
$timeZoneUtc = new \DateTimeZone('UTC');
$dateUTC = new \DateTime('now', $timeZoneUtc);
// America/Sao_Paulo offset is -02:00 (-7200 seconds)
$timeZoneSP = new \DateTimeZone('America/Sao_Paulo');
$dateSP = new \DateTime('now', $timeZoneSP);
// America/New_York offset is -05:00 (-18000 seconds)
$timeZoneNY = new \DateTimeZone('America/New_York');
$dateNY = new \DateTime('now', $timeZoneNY);
$this->assertEquals(0, $timeZoneUtc->getOffset($dateUTC)); // true
$this->assertEquals(0, $timeZoneUtc->getOffset($dateNY)); // true
$this->assertEquals(0, $timeZoneUtc->getOffset($dateSP)); // true
$this->assertEquals(-7200, $timeZoneSP->getOffset($dateUTC)); // true
$this->assertEquals(-7200, $timeZoneSP->getOffset($dateNY)); // true
$this->assertEquals(-7200, $timeZoneSP->getOffset($dateSP)); // true
$this->assertEquals(-18000, $timeZoneNY->getOffset($dateUTC)); // true
$this->assertEquals(-18000, $timeZoneNY->getOffset($dateNY)); // true
$this->assertEquals(-18000, $timeZoneNY->getOffset($dateSP)); // true
}
Can anyone help me get that?

The offset changes only during a DST transition. New York will transition from winter time to summer time on March 11. getOffset tells you what the offset for that timezone is at that particular time. All the times you're giving it are all today, well before the DST transition date, so they all result in the same offset. Try getting the offset for a date before and after March 11 to see a difference.

A date is an absolute moment in time, it doesn't depend on a timezone. The timezone is used only when the date is formatted as string.
Internally, all three DateTime objects created by the code store the same value: the number of seconds (and microseconds on PHP 7) since 1970-01-01 00:00:00 UTC.

Related

PHP: Get timezone identifier offset from UTC

I know \DateTimeZone::listIdentifiers() returns all timezone identifiers, but how can I retrieve their actual offset from UTC?
There is method getoffset
<?php
// Create two timezone objects, one for Taipei (Taiwan) and one for
// Tokyo (Japan)
$dateTimeZoneTaipei = new DateTimeZone("Asia/Taipei");
$dateTimeZoneJapan = new DateTimeZone("Asia/Tokyo");
// Create two DateTime objects that will contain the same Unix timestamp, but
// have different timezones attached to them.
$dateTimeTaipei = new DateTime("now", $dateTimeZoneTaipei);
$dateTimeJapan = new DateTime("now", $dateTimeZoneJapan);
// Calculate the GMT offset for the date/time contained in the $dateTimeTaipei
// object, but using the timezone rules as defined for Tokyo
// ($dateTimeZoneJapan).
$timeOffset = $dateTimeZoneJapan->getOffset($dateTimeTaipei);
// Should show int(32400) (for dates after Sat Sep 8 01:00:00 1951 JST).
var_dump($timeOffset);
?>
You can build an associative array mapping timezones and their offset using the getOffset method of DateTime instance.
getOffset returns the difference in seconds of a date evaluated in the time zone of the provided datetime argument (in our example, this is the current time in utc), and the same datetime as evaluated in the local time zone.
$utcNow = new DateTime('now', (new DateTimeZone('UTC')));
$identOffset = array_map(
function ($ident) use ($utcNow) {
$localTimeZone = new DateTimeZone($ident);
$offset = $localTimeZone->getOffset($utcNow);
return [$ident => $offset] ;
},
DateTimeZone::listIdentifiers()
);
$identOffset = array_merge(...$identOffset);
var_dump($identOffset);
If you are using https://carbon.nesbot.com/
// You will get timezone offset as string
Carbon::now('Asia/Kathmandu')->getOffsetString(); // +05:45

DateTimeZone only handles offsets in one direction

I am attempting to retrieve a date that is not in GMT and convert it to GMT. To do this, I am creating two time zones (one GMT and one non-GMT) and attempting to get the offset between them. However, the offset is only correct in one direction. For this specific example, I am trying to compare GMT +4 to GMT. I expect to get 4hrs (14400 seconds) when I compare the the GMT timezone to the GMT+4 timezone, and -4hrs (-14400 seconds) when I compare the GMT+4 timezone to GMT. However, when comparing the later I'm getting 0... Here is what I have
$default_timezone = new DateTimeZone(drupal_get_user_timezone());
$default_reg_date = new DateTime($reg_date_string, $default_timezone);
$gmt_timezone = new DateTimeZone('UTC');
$gmt_reg_date = new DateTime($reg_date_string, $gmt_timezone);
// Returns as 14400
$default_gmt_offset = $default_timezone->getOffset($gmt_reg_date);
// Returns as 0
$gmt_default_offset = $gmt_timezone->getOffset($default_reg_date);
Why can't I get the right number, what am I doing wrong? Does retrieving the offset only work one way?
Note: in this specific example, drupal_get_user_timezone() is returning GMT+4
From the PHP documentation :
This function returns the offset to GMT for the date/time specified in the datetime parameter. The GMT offset is calculated with the timezone information contained in the DateTimeZone object being used.
The function works with 2 logics steps :
Convert the date into the timezone on which the function is applied
Get the offset from GMT
So calling this function on new DateTimeZone('UTC') will always returns 0
If you want to convert a date into UTC, you can use the setTimeZone function :
$date_string = '2020-05-01 09:11:00' ;
$date = new DateTime($date_string, new DateTimeZone('Europe/Brussels'));
echo $date->format('c') ; // 2020-05-01T09:11:00+02:00
$date->setTimeZone(new DateTimeZone('UTC'));
echo $date->format('c') ; // 2020-05-01T07:11:00+00:00

knowing timezones native offset (when DST is not active) in php

I want to list for the user, all timezones with their native UTC/GMT offset, regardless of DST
How can I do it?
I 've come up with this function to do the job:
function standard_tz_offset($timezone) {
$now = new DateTime('now', $timezone);
$year = $now->format('Y');
$startOfYear = new DateTime('1/1/'.$year, $timezone);
$startOfNext = new DateTime('1/1/'.($year + 1), $timezone);
$transitions = $timezone->getTransitions($startOfYear->getTimestamp(),
$startOfNext->getTimestamp());
foreach($transitions as $transition) {
if(!$transition['isdst']) {
return $transition['offset'];
}
}
return false;
}
How it works
The function accepts a timezone and creates two DateTime objects: January 1st 00:00 of the current year and January 1st 00:00 of the next year, both specified in that timezone.
It then calculates the DST transitions during this year, and returns the offset for the first transition it finds where DST is not active.
PHP 5.3 is required because of the call to DateTimeZone::getTransitions with three parameters. If you want this to work in earlier versions you will have to accept a performance hit, because a whole lot of transitions will be generated by PHP (in this case, you don't need to bother with creating the $startOfYear and $startOfNext dates).
I have also tested this with timezones that do not observe DST (e.g. Asia/Calcutta) and it works for those as well.
To test it:
$timezone = new DateTimeZone("Europe/Athens");
echo standard_tz_offset($timezone);

PHP: Why is this code erroneously returning zero?

I'm calculating the offset between two timezones, but I'm seeing a result I don't expect (zero) with the following code:
$datetimezone_london = new DateTimeZone('Europe/London');
$datetimezone_client = new DateTimeZone('Australia/Canberra');
$now_client = new DateTime("now", $datetimezone_client);
$offset = $datetimezone_london->getOffset($now_client);
echo $offset;
If I flip the timezone strings, it works, but surely the above code should work too. What's happening?
getOffset() returns the offset to GMT in seconds, and London is currently on GMT, hence the return value is zero.
I believe what you need instead is:
$tz_london = new DateTimeZone('Europe/London');
$tz_client = new DateTimeZone('Australia/Canberra');
$time_london = new DateTime('now', $tz_london);
$time_client = new DateTime('now', $tz_client);
$offset = $time_client->getOffset() - $time_london->getOffset();
echo $offset;
This currently (in January) returns 39600 (11 hours). In July it returns 9 hours, and in mid October (where there's a short period when both Europe and Australia are in daylight saving) it returns 10 hours.
DateTimeZone::getOffset() works a bit differently that what most people think. It calculates the offset to GMT of the instance DateTimeZone offset for the date passed as parameter. The date passed as parameter is then converted to the same timezone as the instance (DST and other rules applying) and the offset is calculated for that date.
So your code right now calculates the offset to GMT of the timezone Europe/London.. Since Europe/London is on GMT right now (versus BMT), you are getting 0. (Try a date in August, you'll get 36000).
If you want the current difference between two timezones, use this code...
function timezone_diff($origin, $compareTo, $forDate = "now") {
$dtzOrigin = new DateTimeZone($origin);
$dtzCompareTo = new DateTimeZone($compareTo);
$compareDate = new DateTime($forDate);
$offsetOrigin = $dtzOrigin->getOffset($compareDate);
$offsetCompareTo = $dtzCompareTo->getOffset($compareDate);
return $offsetCompareTo - $offsetOrigin;
}

How to tell if a timezone observes daylight saving at any time of the year?

In PHP, you can tell if a given date is during the Daylight Savings Time period by using something like this:
$isDST = date("I", $myDate); // 1 or 0
The problem is that this only tells you whether that one point in time is in DST. Is there a reliable way to check whether DST is in effect at any time in that timezone?
Edit to clarify:
Brisbane, Australia does not observe daylight savings at any time of the year. All year around, it is GMT+10.
Sydney, Australia does, from October to March when it changes from GMT+10 to GMT+11.
I'm wondering if there would be some existing method, or a way to implement a method which works as such:
timezoneDoesDST('Australia/Brisbane'); // false
timezoneDoesDST('Australia/Sydney'); // true
I've found a method which works using PHP's DateTimezone class (PHP 5.2+)
function timezoneDoesDST($tzId) {
$tz = new DateTimeZone($tzId);
$trans = $tz->getTransitions();
return ((count($trans) && $trans[count($trans) - 1]['ts'] > time()));
}
or, if you're running PHP 5.3+
function timezoneDoesDST($tzId) {
$tz = new DateTimeZone($tzId);
return count($tz->getTransitions(time())) > 0;
}
The getTransitions() function gives you information about each time the offset changes for a timezone. This includes historical data (Brisbane had daylight savings in 1916.. who knew?), so this function checks if there's an offset change in the future or not.
Actually nickf method didn't works for me so I reworked it a little ...
/**
* Finds wherever a TZ is experimenting dst or not
* #author hertzel Armengol <emudojo # gmail.com>
* #params string TimeZone -> US/Pacific for example
*
*/
function timezoneExhibitsDST($tzId) {
$tz = new DateTimeZone($tzId);
$date = new DateTime("now",$tz);
$trans = $tz->getTransitions();
foreach ($trans as $k => $t)
if ($t["ts"] > $date->format('U')) {
return $trans[$k-1]['isdst'];
}
}
// Usage
var_dump(timezoneExhibitsDST("US/Pacific")); --> prints false
var_dump(timezoneExhibitsDST("Europe/London")); --> prints false
var_dump(timezoneExhibitsDST("America/Chicago")); --> prints false
same function call will return true in 1 month (March) hope it helps
DateTimeZone::getTransitions might help.
You could probably wing it:
$hasDst = date("I", strtotime('June 1')) !== date("I", strtotime('Jan 1'));
Otherwise you'd need to parse the text-based zoneinfo data files.
I don't think so, but since almost every country that observes DST changes its time for an entire season or two, you could try to test 4 points during any given year.
For example, test date("I", $date) for 2009/01/01, 2009/04/01, 2009/07/01 and 2009/10/01. If that timezone falls into DST, then at least one of those dates will return 1.
date has to be on the user/server timezone for it to work, and you can't use a range with date as you do with getTransitions

Categories