如何列出两个日期之间的所有月份

我正在尝试列出两个日期之间的所有月份。


例如; 开始日期是:2010-12-02,最后日期是:2012-05-06


我想列出这样的东西:


2010-12

2011-01

2011-02

2011-03

2011-04

.

.

.

2012-04

2012-05

这是我尝试过的,但根本没有用:


    $year_min = 2010;

    $year_max = 2012;

    $month_min = 12;

    $month_max = 5;

    for($y=$year_min; $y<=$year_max; $y++)

    {

        for($m=$month_min; $m<=$month_max; $m++)

        {

            $period[] = $y.$m;

        }

    }


倚天杖
浏览 577回答 3
3回答

眼眸繁星

PHP 5.3$start&nbsp; &nbsp; = new DateTime('2010-12-02');$start->modify('first day of this month');$end&nbsp; &nbsp; &nbsp; = new DateTime('2012-05-06');$end->modify('first day of next month');$interval = DateInterval::createFromDateString('1 month');$period&nbsp; &nbsp;= new DatePeriod($start, $interval, $end);foreach ($period as $dt) {&nbsp; &nbsp; echo $dt->format("Y-m") . "<br>\n";}PHP 5.4或更高版本$start&nbsp; &nbsp; = (new DateTime('2010-12-02'))->modify('first day of this month');$end&nbsp; &nbsp; &nbsp; = (new DateTime('2012-05-06'))->modify('first day of next month');$interval = DateInterval::createFromDateString('1 month');$period&nbsp; &nbsp;= new DatePeriod($start, $interval, $end);foreach ($period as $dt) {&nbsp; &nbsp; echo $dt->format("Y-m") . "<br>\n";}我们将开始日期和结束日期修改为每月第一天的部分很重要。如果我们不这样做,则当前日期要比2月的最后一天高(例如,非-年为28,leap年为29),则跳过2月。

慕斯709654

您必须在同一年的两个月与不同年份的两个月之间有所不同。$year_min = substr($row['contractStart'], 0, 4);$year_max = substr($row['contractEnd'], 0, 4);$month_min = substr($row['contractStart'], 5, 2);$month_min = substr($row['contractEnd'], 5, 2);$period = array();try {&nbsp; if ($year_min > $year_max)&nbsp; &nbsp; throw new Exception();&nbsp; else if ($year_min == $year_max)&nbsp; &nbsp; if ($month_min > $month_max)&nbsp; &nbsp; &nbsp; throw new Exception();&nbsp; &nbsp; for ($month = $month_min; $month <= $month_max; $month++) {&nbsp; &nbsp; &nbsp; $period[] = $month . '-' . $year;&nbsp; &nbsp; }&nbsp; else {&nbsp; &nbsp; for ($month = $month_min; $month <= 12; $month++) {&nbsp; &nbsp; &nbsp; $period[] = $month . '-' . $year_min;&nbsp; &nbsp; }&nbsp; &nbsp; for ($year = $year_min + 1; $year < $year_max; $year++) {&nbsp; &nbsp; &nbsp; for ($month = $month_min; $month <= $month_max; $month++) {&nbsp; &nbsp; &nbsp; &nbsp; $period[] = $month . '-' . $year;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; for ($month = 1; $month <= $month_max; $month++) {&nbsp; &nbsp; &nbsp; $period[] = $month . '-' . $year_max;&nbsp; &nbsp; }&nbsp; }&nbsp; implode("<br />\r\n", $period);}catch (Exception $e) {&nbsp; echo 'Start date occurs after end date.'}那是艰难的方式。现在,已经有一种快速简便的方法作为答案,我建议您选择该方法。

慕容3067478

&nbsp; &nbsp;function getMonthsInRange($startDate, $endDate) {$months = array();while (strtotime($startDate) <= strtotime($endDate)) {&nbsp; &nbsp; $months[] = array('year' => date('Y', strtotime($startDate)), 'month' => date('m', strtotime($startDate)), );&nbsp; &nbsp; $startDate = date('d M Y', strtotime($startDate.&nbsp; &nbsp; &nbsp; &nbsp; '+ 1 month'));}return $months;}
打开App,查看更多内容
随时随地看视频慕课网APP