1

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.

j08691
  • 204,283
  • 31
  • 260
  • 272
Ben
  • 6,026
  • 11
  • 51
  • 72
  • Yes indeed, a UTC offset doesn't say anything about which country it is, which means you cannot really determine the DST setting. – deceze Aug 23 '12 at 14:55
  • Thanks, I think I have found my solution - http://stackoverflow.com/questions/1586552/how-to-tell-if-a-timezone-observes-daylight-saving-at-any-time-of-the-year – Ben Aug 23 '12 at 15:02

2 Answers2

1

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

Colin Pickard
  • 45,724
  • 13
  • 98
  • 148
  • Small warning: in your example $offset will hold the complete offset towards GMT+0, not just the amount that DST adds to it. So in this case it works, because you're using London as your timezone. But if you were to use Athens (GMT+3 in the summer), $offset would 10800 (3 hours), not 3600 (the 1 hour DST offset) I'm still working on this myself, but I think the best way to see if DST is *active*, is using DateTime's format('I'). – Dieter Apr 24 '13 at 16:32
  • Thanx this simplified some thing for me. – Esocoder Aug 27 '18 at 00:13
0

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.

Steven Van Ingelgem
  • 872
  • 2
  • 9
  • 25