Loop through dates with PHP

后端 未结 4 2043
粉色の甜心
粉色の甜心 2021-02-15 11:53

I am trying to loop through dates with PHP. Currently my code gets stuck in a loop repeating 110307. I need the date format to be in yymmdd. Here is what I was trying to use:

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-15 12:12

    strtotime interprets "100227" as the time 10:02:27 today, not 2010-02-27. So after the first step, $check_date (today) is "110307". At all subsequent steps "110307" is again interpreted as a time today, giving $check_date as "110307" again.

    A neat trick for iterating dates is to take advantage of mktime's ability to normalize dates, something like this:

    $date_arr = array(27,2,2010);
    $end_date = "100324";
    do {
        $check_date = gmdate('ymd', gmmktime(0,0,0,$date_arr[1],$date_arr[0]++,$date_arr[2]));
        echo $check_date."\n";
    } while($end_date!=$check_date);
    

提交回复
热议问题