Ben
Ben

Reputation: 6086

Calculate if DST active for a given timestamp and offset

Given a unix timestamp and timezone offset relative to UTC (e.g. -5), is it possible to calculate if DST is active?

I can do something like:

<?php
  echo 'DST enabled? ' . date('I', 1345731453) ."<br>"; 
?>

But I cannot see how to pass in an offset or a TimeZone.

Im guessing also that there is a difference between the timezone and the offset, given that specific countries support DST?

Appreciate any thoughts.

Upvotes: 1

Views: 893

Answers (2)

Colin Pickard
Colin Pickard

Reputation: 46653

Correct, you need to start with date and tz to determine offset.

in 5.3+ you can get transitions for start/end period, so you can just use your timestamp for both, something like this:

$inputTime = $your_unix_timestamp;
$inputTZ = new DateTimeZone('Europe/London'); 

$transitions = $inputTZ->getTransitions($inputTime,$inputTime); 
$offset = $transitions[0]['offset'];

$offset is the DST offset at the time in question

Upvotes: 1

Steven Van Ingelgem
Steven Van Ingelgem

Reputation: 1009

Get the timezone with:

$old_zone = ini_get('date.timezone');

Change it to the one you want:

ini_set('date.timezone', 'Europe/Brussels');

Revert back:

ini_set('date.timezone', $old_zone);

The PHP timestamp is always UTC. Depending on your timezone setting it determine what time to show.

Upvotes: 0

Related Questions