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

Php 如何列出两个日期之间的所有月份,php,date,datetime,Php,Date,Datetime,我试着列出两次约会之间的所有月份 比如,;开始日期: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;

我试着列出两次约会之间的所有月份

比如,;开始日期: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;
        }
    }
PHP5.3

PHP5.4或更新版本


我们将开始和结束日期修改为月初的部分很重要。如果我们不这样做,那么当前日期将高于2月的最后一天,即非闰年为28,闰年为29,这将跳过2月。

您必须在同一年的两个月和不同年份的两个月之间进行区分

$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.'
}

这是艰难的道路。现在有一个简单快捷的方法,我建议您选择。这是我的解决方案,因为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++;
}
试试看:

在拉威尔

$period = \Carbon\CarbonPeriod::create('2017-06-28', '1 month', '2019-06-01');

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

我就是这样。我好极了!。这就解决了问题,值得一试,只要看到有人使用datetime对象和DateInterval实现简单的解决方案。。。尽管我对你的销售代表的期望不会降低:我喜欢改进后的部件。太干净了。这绝对是答案。PHP5.4或更高版本的版本中没有错误吗?在低于5.4的版本中,有$end->modify'first day of next month';5.4以上为->修改“本月第一天”@Martin只是把结果放在一个数组中,然后反过来,我不得不投票给你,感谢你花时间用他们提供的代码来制定解决方案非常感谢,我做得很开心。幸运的是,您没有在代码中选择我的答案:-P!你能解释一下为什么这是有用的吗?这一个在第一次尝试时效果很好。我喜欢这个解决方案,非常适合我的需要。泰
$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++;
}
    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('01 M Y', strtotime($startDate.
        '+ 1 month')); // Set date to 1 so that new month is returned as the month changes.
}

return $months;
}
$period = \Carbon\CarbonPeriod::create('2017-06-28', '1 month', '2019-06-01');

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