CALCULATE THE NUMBER OF MONTHS BETWEEN TWO DATES IN PHP?

Posted on 

https://stackoverflow.com/questions/13416894/calculate-the-number-of-months-between-two-dates-in-php/25684828

$date1 = '2000-01-25';
$date2 = '2010-02-20';

$ts1 = strtotime($date1);
$ts2 = strtotime($date2);

$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);

$month1 = date('m', $ts1);
$month2 = date('m', $ts2);

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

You may want to include the days somewhere too, depending on whether you mean whole months or not. Hope you get the idea though.

 

Post a Comment

0 Comments