PHP: Loop through all months in a date range?

后端 未结 7 1044
误落风尘
误落风尘 2020-12-02 13:32

If I have a start date (say 2009-02-01) and an end date (say 2010-01-01), how can I create a loop to go through all the dates (months) in the range

7条回答
  •  有刺的猬
    2020-12-02 13:44

    I like the simplicity of the accepted answer, but as 3s2ng, it doesn't always work. So I tweeked it like this:

        $start = strtotime('2009-02-01');
        $startmoyr = date('Y', $start) . date('m', $start);
        $end = strtotime('2013-12-01');
        $endmoyr = date('Y', $end) . date('m', $end);
    
        while ($startmoyr <= $endmoyr) {
            echo date("F Y", $start) . "
    "; $start = strtotime("+1month", $start); $startmoyr = date('Y', $start) . date('m', $start); }

提交回复
热议问题