Is this a PHP date() bug, or is there something wrong with my code?

后端 未结 2 1894
后悔当初
后悔当初 2020-12-04 02:27

I\'ve two images of an arrow, one which increments the month backward, one which increments it forward via href.

if (ISSET($_GET[\"month\"])){
    $month_in         


        
2条回答
  •  遥遥无期
    2020-12-04 03:11

    There is no February 29th in 2015.

    By just adding or subtracting whole months at a time, you create new dates on the same day in the requested month. In this case, you caused PHP to try and make a February 29, 2015 date. It automatically jumped forward to March 1, 2015.

    If you only care about Months, create dates on the first of each month:

    date("F y", mktime(0,0,0, $month_index, 1, 2015));
    

    Good thing you are writing this code and caught the bug today, or you would have had a bug that only appeared on the 29th (or 31st) of every month (except leap years)!

    Dates are hard.

提交回复
热议问题