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

时间:2014-05-09 08:47:17

标签: php

我有2个约会。我希望每个月都能获得总天数。

我如何在PHP中执行此操作?

例如

$date1 = '2013-11-13'; // yy-mm-dd format
$date2 = '2014-02-14';

输出

Months      Total Days
-----------------------
11-2013     30  
12-2013     31
01-2014     31
02-2014     28

6 个答案:

答案 0 :(得分:2)

试试:

$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);

输出:

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)

答案 1 :(得分:2)

尝试下面给出的代码:

          $date1 = '2013-11-15'; // yy-mm-dd format
          $date2 = '2014-02-15';
          $start    = new DateTime($date1);

          $start->modify('first day of this month');
          $end      = new DateTime($date2);
          $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") ."  " ;
                 echo cal_days_in_month(CAL_GREGORIAN,$dt->format("m"),$dt->format("Y")) . "<br/>";
          }

另外,结帐this链接了解更多

答案 2 :(得分:1)

我使用了时间戳和日期:

$date1 = '2013-11-15'; // yy-mm-dd format
$date2 = '2014-02-15';

$d1 = strtotime('2013-11-15');
$d2 = strtotime('2014-02-15');

while ($d1 <= $d2) {
    echo date('m-d-Y', $d1)." | ";
    echo cal_days_in_month(CAL_GREGORIAN, date('m', $d1), date('Y', $d1)) ."<br>";
    $d1 = strtotime("+1 month", $d1);

}

答案 3 :(得分:0)

这就是我提出的:

$arr_months = array();
$date1 = new DateTime('2013-11-15');
$date2 = new DateTime('2014-02-15');
$month1 = new DateTime($date1->format('Y-m')); //The first day of the month of date 1
while ($month1 < $date2) { //Check if the first day of the next month is still before date 2
    $arr_months[$month1->format('Y-m')] = cal_days_in_month(CAL_GREGORIAN, $month1->format('m'), $month1->format('Y')); //Add it to the array
    $month1->modify('+1 month'); //Add one month and repeat
    }

print_r($arr_months);

它创建一个关联数组,其中月份为键,天数为值。 从示例创建的数组将是:

Array
(
    [2013-11] => 30
    [2013-12] => 31
    [2014-01] => 31
    [2014-02] => 28
)

使用foreach循环,您将能够轻松地滚动数组。

答案 4 :(得分:0)

这应该可以帮到你,查看,

$date1 = new DateTime('2013-11-15'); 
$date1->modify('first day of this month');
$date2 = new DateTime('2014-02-15');
$date2->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$value  = new DatePeriod($date1, $interval, $date2);


foreach ($value as $dates) {
  echo $dates->format("m- Y")."-->".cal_days_in_month(0,$dates->format("m"),$dates->format("Y"))."<br>\n";    
}

答案 5 :(得分:0)

您可以像这样使用DateTime课程和cal_day_in_month()

$datetime1 = "2014-02-15";
$datetime2= "2013-03-15";

$date1 = new DateTime($datetime1);
$date2 = new DateTime($datetime2);

while (date_format($date2, 'Y-m') <= date_format($date1, 'Y-m'))
{

    $date2 = $date2->add(new DateInterval('P1M'));

    echo $date2->format('Y-m')." | ".cal_days_in_month(CAL_GREGORIAN, $date2->format('m'), $date2->format('Y'))."<br>";

}