Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP and the end of October

I have found the weirdest error. And I'm not asking for a fix, I'm just curious if you guys experienced the same and leave it for future reference.

I have the following code, which should output every day of the year 2016

$day = "2016-01-01";
for($i = 1; $i<= 365; $i++){
  $day = date("Y-m-d", strtotime($day)+60*60*24);
  echo $day."<br/>";    
}

But after 30-10-2016 it stops increasing and displays the same date.

PHP Version is 5.6.22. This happens for every year, not only 2016. Any idea what is causing this? Is this a problem with this specific PHP Version? Is this a timezone problem? Have you experienced the same, or something similar?

What interests me is the cause of this problem. There seems to be something wrong with the strtotime function.

like image 920
chillichief Avatar asked Dec 02 '25 09:12

chillichief


1 Answers

Adding seconds to timestamps to change days/weeks/months can have unexpected results. Try explicitly adding one day...

$day = "2016-01-01";
for($i = 1; $i<= 365; $i++){
   $day = date("Y-m-d", strtotime("+ 1 day", strtotime($day)));
   echo $day.PHP_EOL;    
}

Which timezone is your PHP set to, is there a daylight savings time change at the end of October? - That will cause the date to stick as the day may be 25 hours long.

like image 153
Steve Avatar answered Dec 03 '25 23:12

Steve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!