Miklos Miklos -4 years ago 88
PHP Question

PHP daylight saving always false

I would like to know whether a date is in daylight saving time.

So I do this:

echo date('I', strtotime('2017-03-23')); // outputs 0
echo date('I', strtotime('2017-03-29')); // outputs 0


I expect to have different results for the two inputs, as the switch in the UK is 26 March 2017, however the output is 0 in both cases.

I have checked and the time zone looks correct:

echo date_default_timezone_get(); // outputs UTC

Answer Source

As you can see in this answer, UTC does not have daylight saving, you need to set your timezone to Europe/London :

date_default_timezone_set('Europe/London'); 
echo date('I', strtotime('2017-03-23')); // echoes 0
echo date('I', strtotime('2017-03-29')); // echoes 1

date_default_timezone_set('UTC'); 
echo date('I', strtotime('2017-03-23')); // echoes 0
echo date('I', strtotime('2017-03-29')); // echoes 0
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download