2

I need to calculate months from two dates

For example :

If date1 = '2014-08-20' means 20th Aug 2014

& date2 = '2014-09-17' means today date

then I need to difference of months = 2 (Aug + September)

Same way

     If `date1 = '2014-03-15'` means 15th March 2014

     &  `date2 =  '2014-09-17'`  means today date

then I need to difference of months = 7 (March + April + May + June + July + Aug + SEPT)

How can I do with php date functions ?

Sandip Patel
  • 45
  • 1
  • 8
  • Please check, may be duplicate :- http://stackoverflow.com/questions/13416894/calculate-the-number-of-months-between-two-dates-in-php – Khushboo Sep 17 '14 at 06:32
  • possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Clément Malet Sep 17 '14 at 06:32

5 Answers5

2
$date1 = new DateTime('2014-08-20');
$date2 = new DateTime('2014-09-17');

$diff = $date2->diff($date1);

$diff->format('%m months');
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
2

If you want to take into account for each months passed, you could try this:

$date1 = new DateTime('2014-03-15');
$date2 = new DateTime();
$date2->modify('last day of this month'); // adjust it to take into account
$int = new DateInterval('P1M'); // every month
$count = 0;
$range = new DatePeriod($date1, $int, $date2);
foreach($range as $d) {
    ++$count; // for each month incremenent
}
echo $count;
Kevin
  • 41,694
  • 12
  • 53
  • 70
2

This is what you are looking for?

$date1 = new DateTime('2014-08-20');
$date2 = new DateTime('2014-09-17');

echo  $date2->format('n') - $date1->format('n') + 1;
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
0

You can do with:

$first_date = '2014-03-15';
$second_date = '2014-09-17';

$date1 = strtotime($first_date);
$date2 = strtotime($second_date);

$year1 = date('Y', $date1);
$year2 = date('Y', $date2);

$month1 = date('m', $date1);
$month2 = date('m', $date2);

$diff = (($year2 - $year1) * 12) + ($month2 - $month1);

But to count day difference also, we can do as follow:

$date_interval = $first_date->diff($second_date);
        echo "difference " . $date_interval->y . " years, " . $date_interval->m." months, ".$date_interval->d." days "; 

Check this link link

Community
  • 1
  • 1
Mobasher Fasihy
  • 1,021
  • 2
  • 9
  • 17
0

You can Do it in the following manner.

Consider this Example :

<?php
$start    = new DateTime('2011-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) {
    $monthNum=$dt->format("m");
    echo $dt->format("Y").date('F', mktime(0, 0, 0, $monthNum, 10)). PHP_EOL;
}

See in action Here

Reference : How to list all months between two dates

Community
  • 1
  • 1
OshoParth
  • 1,492
  • 2
  • 20
  • 44