-7

I have 2 dates. I want to get all months with total days in each.

How can I do this in PHP?

For example

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

Output

Months      Total Days
-----------------------
11-2013     30  
12-2013     31
01-2014     31
02-2014     28
halfer
  • 19,824
  • 17
  • 99
  • 186
Anand Solanki
  • 3,419
  • 4
  • 16
  • 27
  • Create proper `DateTime` objects, and then use a loop that adds a month on the start date while less than the end date … – CBroe May 09 '14 at 08:58

6 Answers6

8

Just try with:

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

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)
hsz
  • 148,279
  • 62
  • 259
  • 315
  • 1
    What if start date is 2015-01-30 this will never give second month, You must use base date for date calculation in `strtotime`. Use `$date1 = date('m-Y',strtotime('2013-11-15')).'-01';` – mukund Apr 05 '15 at 15:13
2

Try the below given code :

          $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/>";
          }

Also, checkout this link for more

1

i used timestamp and date:

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

}
Viscocent
  • 2,024
  • 2
  • 19
  • 26
1

This should help you, Check out,

$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";    
}
vivek
  • 36
  • 7
0

This is what i came up with:

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

It creates an associative array with the month as key and the number of days as value. The array created from the example would be:

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

With a foreach loop you will be able to scroll trough the array easily.

Thisis
  • 17
  • 3
0

You can use the DateTime class along with cal_day_in_month() like this

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

}
krishna
  • 4,069
  • 2
  • 29
  • 56