Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - using date to find out daylight savings time

I want to give date() a date and have it return if that date is DST or not. Running PHP 5.4.7 via xampp on a windows 7 box. A linux box running PHP 5.2.8 returns 0 no matter what date I give it.

What's wrong with my code?

echo date('I', strtotime('30-Mar-2013'));
# should return a 1 but returns 0

echo date('I', strtotime('30-Mar-2013 America/Los_Angeles'))."<br>"; # returns 0
echo date('I', strtotime('31-Mar-2013 America/Los_Angeles'))."<br>"; # returns 1

The switch between DST should be between 9-Mar-2013 - 10-Mar-2013.

At this point, the question is, why doesn't my PHP code return 1.

like image 696
user2001487 Avatar asked Apr 29 '13 21:04

user2001487


People also ask

How does PHP determine daylight Savings?

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.

How is the date for daylight savings time determined?

By the Energy Policy Act of 2005, daylight saving time (DST) was extended in the United States beginning in 2007. As from that year, DST begins on the second Sunday of March and ends on the first Sunday of November.

Does PYTZ account for daylight savings?

pytz will help you to tell if an date is under DST influence by checking dst() method.

What were the old dates for daylight savings time?

For years, the US observed DST from the first Sunday of April to the last Sunday of October. In 2005, President George W. Bush extended DST an extra four weeks, officially taking effect in 2007.


1 Answers

You can't use strtotime because it creates a UTC timestamp, which removes the timezone information. Instead, just use DateTime

$date = new DateTime('30-Mar-2013 America/Los_Angeles');
echo $date->format('I');
# 1

$date = new DateTime('31-Mar-2013 America/Los_Angeles');
echo $date->format('I');
# 1

$date = new DateTime('09-Mar-2013 America/Los_Angeles');
echo $date->format('I');
# 0

$date = new DateTime('10-Mar-2013 America/Los_Angeles');
echo $date->format('I');
# 0

Notice that the DST is still 0 on that last one? That's because the transition happens at 2:00 AM:

$date = new DateTime('10-Mar-2013 01:00 America/Los_Angeles');
echo $date->format('I');
# 0

$date = new DateTime('10-Mar-2013 02:00 America/Los_Angeles');
echo $date->format('I');
# 1

This is a "spring-forward" transition, where the clock jumps from 2:00 to 3:00.

Beware of ambiguity during fall-back transitions, where the clock jumps from 2:00 back to 1:00

$date = new DateTime('3-Nov-2013 01:00 America/Los_Angeles');
echo $date->format('I');
# 1

There are two 1:00 AMs, and this is taking the first one. It is ambiguous, because we might have meant the second one, and that is not represented.

One would think that PHP would allow either of the following:

new DateTime('3-Nov-2013 01:00 -0700 America/Los_Angeles')  #PDT
new DateTime('3-Nov-2013 01:00 -0800 America/Los_Angeles')  #PST

But these don't seem to work when I tested. I also tried ISO format dates. Anyone know how to properly distinguish ambiguous values in PHP? If so, please edit or update in comments. Thanks.

like image 191
Matt Johnson-Pint Avatar answered Sep 21 '22 16:09

Matt Johnson-Pint