开发者

Loop through all months in a date range?

开发者 https://www.devze.com 2022-12-18 13:41 出处:网络
If I have a start date (say 2009-02-01) and an end date (say 2010-01-01), how can 开发者_Python百科I create a loop to go through all the dates (months) in the range?Try

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


Try

$start = $month = strtotime('2009-02-01');
$end = strtotime('2011-01-01');
while($month < $end)
{
     echo date('F Y', $month), PHP_EOL;
     $month = strtotime("+1 month", $month);
}

Mind the note http://php.net/manual/de/datetime.formats.relative.php

Relative month values are calculated based on the length of months that they pass through. An example would be "+2 month 2011-11-30", which would produce "2012-01-30". This is due to November being 30 days in length, and December being 31 days in length, producing a total of 61 days.

As of PHP5.3 you can use http://www.php.net/manual/en/class.dateperiod.php


Example of DateTime, DateInterval and DatePeriod class combination :

$start = new DateTime('2009-02-01');
$interval = new DateInterval('P1M');
$end = new DateTime('2011-01-01');
$period = new DatePeriod($start, $interval, $end);

foreach ($period as $dt) {
    echo $dt->format('F Y') . PHP_EOL;
}


The accepted answer is not the proper way.

I tried this snippet and it does not work properly. If your start date is the end of the month and the end date is the start of the 3rd month.

For example: 2014-08-31 - 2014-10-01

Expected should be.

  • August
  • September
  • October

The better solution is:

$start    = new DateTime('2010-12-02');
$start->modify('first day of this month');
$end      = new DateTime('2012-05-06');
$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $dt) {
    echo $dt->format("Y-m") . "<br>\n";
}

Reference: How to list all months between two dates


$start = strtotime('2011-09-01');
$end = strtotime('2013-12-01');
while($start < $end)
{
    echo date('F Y', $start) . '<br>';
    $start = strtotime("+1 month", $start);
}


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) . "<br>";
        $start = strtotime("+1month", $start);
        $startmoyr = date('Y', $start) . date('m', $start);
    }


Based on Gordon's response this is the way I actually work when you need to get all the months between.

$end = strtotime(date("Y-m-01"));
$start = $month = strtotime("-12 months", $end);

while ( $month < $end ) {
    echo date("Y-m-d", $month);
    $month = strtotime("+1 month", $month);
}

This is the result if I execute this code now:

2018-05-01
2018-06-01
2018-07-01
2018-08-01
2018-09-01
2018-10-01
2018-11-01
2018-12-01
2019-01-01
2019-02-01
2019-03-01
2019-04-01

Notice that this doesn't include the current month. If you need to include current month, you can set the "$end" variable to the first day of the next month.

$current_first_day_of_the_month = date("Y-m-01");
$end = strtotime("$current_first_day_of_the_month +1 month");
$start = $month = strtotime("-12 months", $end);


I have a method which is optimal in results :

$begin = new DateTime( '2014-07-14' );
$end = new DateTime( '2014-08-01' );
$end = $end->modify( '+1 month' );
$interval = DateInterval::createFromDateString('1 month');

$period = new DatePeriod($begin, $interval, $end);

foreach($period as $dt) {
    var_dump($dt->format( "m" ));
}

A plus for the method of @Glavic


This could be useful in a reverse loop (from future to past)

$start = $month = strtotime("2023-07-01"); //You must use the same day date to prevent problems (and we want a loop by months)
$end = strtotime("2021-12-01");

while($month>=$end) { //use > if don't want the last month

  //Insert you code here

  echo date('M Y', $month) . " "; //just to show it works

  //the next line is the key. You can reverse jump by a month and PHP understand changes of year
  $month = mktime(0, 0, 0, date("m", $month)-1, date("d", $month), date("Y", $month) );
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号