Home > Back-end >  How to deal with winter and summer time
How to deal with winter and summer time

Time:11-11

Here is my issue :

Our server is running on a date with a delay;

$now = new \DateTime("now", new \DateTimeZone('Europe/Paris') );

Meaning even if it's actually 4 pm in France, our server is gonna say it's 2pm (or may be 6pm, I can't remember the direction...but we have a 2h difference).

It's not due to php, it's due to our server configuration.

When the clock changed and we went to winter time a few weeks ago...that delta difference went from 2h diff to 1h diff.

Which mean I had to adapt my code

Going from

$nowBeforeDelta = new \DateTime("now", new \DateTimeZone($gmt) );
$now = $nowBeforeDelta->sub(new \DateInterval("PT2H"));

TO

$nowBeforeDelta = new \DateTime("now", new \DateTimeZone($gmt) );
$now = $nowBeforeDelta->sub(new \DateInterval("PT1H"));

This way I'm receiving the hour correctly accordingly to the server...but...you saw where I'm going...once summer time will be coming again...I will have to change again.

So, Here comes my question : Do I have a way to know, given a specific date, if that date is in winter time or summer time ? Not getting the date and hour...just getting a response to the question winter or summer ?

CodePudding user response:

You could check, if there is a daylight saving active in the given timezone.

$date = new DateTime('now', new DateTimeZone('Europe/Berlin'));
$interval = $date->format('I') > 0 ? 'PT2H' : 'PT1H';
$now = $date->sub(new DateInterval($interval));

As mentioned in the documentation, die I checks wether or not the date is in daylight saving time. It returns 0 if in wintertime and 1 if in summertime.

  • Related