如何在PHP中获得两个日期之间的所有月份?

Ana*_*nki -7 php

我有2个约会。我想获得所有月份的总天数。

我怎样才能在 PHP 中做到这一点?

例如

$date1 = '2013-11-13'; // yy-mm-dd format
$date2 = '2014-02-14';
Run Code Online (Sandbox Code Playgroud)

输出

Months      Total Days
-----------------------
11-2013     30  
12-2013     31
01-2014     31
02-2014     28
Run Code Online (Sandbox Code Playgroud)

hsz*_*hsz 7

只需尝试:

$date1  = '2013-11-15';
$date2  = '2014-02-15';
$output = [];
$time   = strtotime($date1);
$last   = date('m-Y', strtotime($date2));

do {
    $month = date('m-Y', $time);
    $total = date('t', $time);

    $output[] = [
        'month' => $month,
        'total' => $total,
    ];

    $time = strtotime('+1 month', $time);
} while ($month != $last);


var_dump($output);
Run Code Online (Sandbox Code Playgroud)

输出:

array (size=4)
  0 => 
    array (size=2)
      'month' => string '11-2013' (length=7)
      'total' => string '30' (length=2)
  1 => 
    array (size=2)
      'month' => string '12-2013' (length=7)
      'total' => string '31' (length=2)
  2 => 
    array (size=2)
      'month' => string '01-2014' (length=7)
      'total' => string '31' (length=2)
  3 => 
    array (size=2)
      'month' => string '02-2014' (length=7)
      'total' => string '28' (length=2)
Run Code Online (Sandbox Code Playgroud)