Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP daylight saving time detection

I need to send an email to users based wherever in the world at 9:00 am local time. The server is in the UK. What I can do is set up a time difference between each user and the server's time, which would then perfectly work if DST didn't exist.

Here's an example to illustrate it:

  • John works in New York, -5 hours from the server (UK) time
  • Richard works in London, UK, so 0 hour difference with the server.

When the server goes from GMT to GMT +1 (BST) at 2:00am on a certain Sunday, this means that John now has a -6H time difference now.

This scenario I can still handle by updating all the users outside the server's local time, but once I've moved forward/backward the time of all the other users, I still need a way to detect when (time and date) the users living outside the UK will (or will not) change their local time to a probable DST one.

I need a PHP method to know/detect when other parts of the world will enter/exit DST.

like image 905
Nicolas Avatar asked May 27 '11 16:05

Nicolas


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'));

How to handle daylight saving time in php?

One method would be to set the timezone in PHP to either GMT or UTC in php. ini or by using date_default_timezone_set() . The other approach would be to using gmdate() in place of date() . Same arguments, a few hours difference in the results.

How does Java determine Daylight Savings?

The observesDaylightTime() method of TimeZone class in Java is used to check and verify whether the given date is in daylight saving time or if any transition from Standard Time to Daylight Saving Time will occur at any future time.


2 Answers

Do you need to know all the details of DST transition yourself? or do you just need to know when is 9:00 am in a given timezone?

If it's the latter, PHP can use your operating system's timezone database to do that for you. The strtotime() function is remarkably good at "figuring out" what you mean:

echo strtotime("today 9:00 am America/New_York");  // prints "1306501200" echo strtotime("today 9:00 am Europe/London");     // prints "1306483200" 

Just make sure you're using one of the PHP supported timezones.

like image 200
squirrel Avatar answered Sep 22 '22 01:09

squirrel


As Jimmy points out you can use timezone transitions, but this is not available on PHP <5.3. as dateTimeZone() is PHP>=5.2.2 but getTransitions() with arguments is not! In that case here is a function that can give you timezone data, including whether in DST or not.

function timezonez($timezone = 'Europe/London'){     $tz = new DateTimeZone($timezone);     $transitions = $tz->getTransitions();     if (is_array($transitions)){         foreach ($transitions as $k => $t){             // look for current year             if (substr($t['time'],0,4) == date('Y')){                 $trans = $t;                 break;             }         }     }     return (isset($trans)) ? $trans : false; } 

Having said that, there is a simpler method using date() if you just need to know whether a timezone is in DST. For example if you want to know if UK is in DST you can do this:

date_default_timezone_set('Europe/London'); $bool = date('I'); // this will be 1 in DST or else 0 

... or supply a timestamp as a second arg to date() if you want to specify a datetime other than your current server time.

like image 30
Owen Avatar answered Sep 22 '22 01:09

Owen