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

Poo*_*oya 57 php datetime date

我想列出两个日期之间的所有月份.

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

我想列出这样的东西:

2010-12
2011-01
2011-02
2011-03
2011-04
.
.
.
2012-04
2012-05
Run Code Online (Sandbox Code Playgroud)

这是我尝试过的,它根本不起作用:

    $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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

Joh*_*nde 182

PHP 5.3

$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";
}
Run Code Online (Sandbox Code Playgroud)

看到它在行动

PHP 5.4或更高版本

$start    = (new DateTime('2010-12-02'))->modify('first day of this month');
$end      = (new DateTime('2012-05-06'))->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";
}
Run Code Online (Sandbox Code Playgroud)

我们将开始和结束日期修改为月初的部分非常重要.如果我们没有,并且当前日高于2月的最后一天(即非闰年28,闰年29),这将超过2月.

  • IMO`his.is("太棒了!")` (18认同)

Ste*_*roz 9

你必须在同一个月的两个月和不同年份的两个月之间做出改变.

$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 {
  if ($year_min > $year_max)
    throw new Exception();
  else if ($year_min == $year_max)
    if ($month_min > $month_max)
      throw new Exception();
    for ($month = $month_min; $month <= $month_max; $month++) {
      $period[] = $month . '-' . $year;
    }
  else {
    for ($month = $month_min; $month <= 12; $month++) {
      $period[] = $month . '-' . $year_min;
    }
    for ($year = $year_min + 1; $year < $year_max; $year++) {
      for ($month = $month_min; $month <= $month_max; $month++) {
        $period[] = $month . '-' . $year;
      }
    }
    for ($month = 1; $month <= $month_max; $month++) {
      $period[] = $month . '-' . $year_max;
    }
  }
  implode("<br />\r\n", $period);
}
catch (Exception $e) {
  echo 'Start date occurs after end date.'
}
Run Code Online (Sandbox Code Playgroud)

这是艰难的方式.现在有一种快速简便的方法已作为答案给出,我建议你选择.

  • 我不得不支持你花时间使用他们提供的代码来制定解决方案 (3认同)

小智 7

    function getMonthsInRange($startDate, $endDate) {
$months = array();
while (strtotime($startDate) <= strtotime($endDate)) {
    $months[] = array('year' => date('Y', strtotime($startDate)), 'month' => date('m', strtotime($startDate)), );
    $startDate = date('d M Y', strtotime($startDate.
        '+ 1 month'));
}

return $months;
}
Run Code Online (Sandbox Code Playgroud)

  • 你能澄清一下为什么这是有帮助的吗? (2认同)

Cpt*_*tAJ 5

这是我的解决方案,因为我的服务器环境中没有DateTime.

$a = "2007-01-01";
$b = "2008-02-15";

$i = date("Ym", strtotime($a));
while($i <= date("Ym", strtotime($b))){
    echo $i."\n";
    if(substr($i, 4, 2) == "12")
        $i = (date("Y", strtotime($i."01")) + 1)."01";
    else
        $i++;
}
Run Code Online (Sandbox Code Playgroud)

试一试:http://3v4l.org/BZOmb