37

How can I detect day light savings automatically in PHP?

Cœur
  • 37,241
  • 25
  • 195
  • 267
SMSM
  • 1,509
  • 3
  • 19
  • 34
  • I can't help you with the answer, at least if you mean if a screen brightned down for energysaving. But where are you going to use it for? Just curious... – Ben Fransen May 04 '10 at 08:54
  • @Ben http://en.wikipedia.org/wiki/Daylight_saving_time – Glycerine May 04 '10 at 09:01
  • 1
    @Glycerine, ah in that way! From the question I understood he wanted to know what energyscheme for eg a laptop was using ;) I'm from Holland, DST is called 'Zomertijd'/'Summertime' here ;) – Ben Fransen May 04 '10 at 09:12
  • 1
    HA! thats funny. is there a 'stackoverrflow bloopers'? – Glycerine May 04 '10 at 09:25

2 Answers2

79
echo date('I');

The I (capital i) is a 1/0 denoting whether or not daylight saving is currently in effect.

http://php.net/manual/en/function.date.php

hope it helps.

dtbarne
  • 8,110
  • 5
  • 43
  • 49
Glycerine
  • 7,157
  • 4
  • 39
  • 65
  • 15
    There is no reason to pass `time()` as the second parameter to `date()`, it uses the current time by default: http://www.php.net/manual/en/function.date.php – Adrian Apr 02 '13 at 18:10
  • 5
    I want to note here that is specific to the default or set timezone `(date_default_timezone_set('string');`. So for example if the timezone was in Arizona, then it would always return false as there is no DST in Arizona. –  Jul 07 '15 at 06:33
  • 1
    @user1893702 So how do you get daylight saving for a specific timezone? – Max Feb 13 '20 at 15:38
  • 4
    @Max, this is possible using the DateTime interface. ```php $date = new DateTime('now', new DateTimeZone('America/Phoenix')); var_dump($date->format('I')); ``` – Sephiroth Mar 28 '21 at 17:43
  • @Sephiroth Thank you! (Though I'm still mad that you killed Aeris) – Max Mar 28 '21 at 17:57
5

This was posted as a comment to another answer. It's the correct answer and would be valuable for others to see. The answer currently marked as correct only works if you want to find out whether it's daylight saving time (DST) in the server/PHP timezone. It doesn't work for just any timezone (or if your server/PHP timezone is set to UTC).

date('I') will return "1" if the server/PHP timezone is in DST, or "0" if it isn't.

To find out if a specific timezone is in DST, use:

$date = new DateTime('now', new DateTimeZone('America/Los_Angeles'));
var_dump($date->format('I'));

Replace America/Los_Angeles with whatever timezone you're curious about.

To generalize this, and to get a boolean instead of a string:

function getIsDaylightSaving($timezoneStr = 'America/Los_Angeles') {
  $date = new DateTime('now', new DateTimeZone($timezoneStr));
  return (bool) $date->format('I');
}

NOTE: You could also use this method to find out if any date/time is in DST for a specific timezone. Just replace DateTime('now') with a specific date/time string. For example, to find out if 11:14pm on December 20th, 2021 is in DST for the USA Eastern Timezone:

$date = new DateTime('2020-12-20 23:14', new DateTimeZone('America/New_York'));
var_dump((bool) $date->format('I')); // returns false

// or a date/time that was in DST
$date = new DateTime('2020-03-20 23:14', new DateTimeZone('America/New_York'));
var_dump((bool) $date->format('I')); // returns true
Cully
  • 6,427
  • 4
  • 36
  • 58