PHP date () will lose an hour from the UNIX era

Hi, I am experiencing strange behavior with php date () function. I am trying to increase the dates by a week at such a time.

CODE:

<?php date_default_timezone_set('Europe/London'); echo 7*24*60*60; echo '<br>'; echo date('d/m/YH:i:s', 0); echo '<br>'; echo date('d/m/YH:i:s', 604800); ?> 

The result is the following result.

OUTPUT:

 604800 01/01/1970 01:00:00 08/01/1970 01:00:00 

As expected, the date increased by 7 days to a second. But after I reach a certain number of seconds, the date function seems to lose an hour from the date.

CODE:

 <?php date_default_timezone_set('Europe/London'); echo (1351468800 - 1350864000); echo '<br>'; echo date('d/m/YH:i:s', 1350864000); echo '<br>'; echo date('d/m/YH:i:s', 1351468800); ?> 

Next release results

OUTPUT:

 604800 22/10/2012 01:00:00 29/10/2012 00:00:00 

As you can see, the date has lost an hour, although the difference between the two dates is 604800 seconds. I tested this on two different servers, and I also tested similar code using a DateTime object, but still the same result. Where am I mistaken?

+4
source share
2 answers

I believe that DST comes into play here. How DST ends on October 28, 2012 in London.

+6
source

Use strtotime more reliable

 date_default_timezone_set('Europe/London'); $startDate = "1350864000" ; $senvenDays = strtotime("+7 day", $startDate); var_dump(date("d/m/YH:i:s",$startDate)); var_dump(date("d/m/YH:i:s",$senvenDays)); var_dump($senvenDays - $startDate); 

Output

 string '22/10/2012 01:00:00' (length=19) string '29/10/2012 01:00:00' (length=19) int 608400 
+3
source

Source: https://habr.com/ru/post/1436420/


All Articles