Loop through dates with PHP

后端 未结 4 1979
粉色の甜心
粉色の甜心 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:26

    Here's how I prefer to do it:

    $startDate = new DateTime('20100227');
    $endDate = new DateTime('20100324');
    
    while ($startDate <= $endDate) {
      // your code here
      ...
      // go to the next day
      $startDate->add(new DateInterval('P1D'));
    }
    

    I find this much cleaner personally, and it's nice not having to hard-code values like 84600.

提交回复
热议问题