Without using PHP 5.3\'s date_diff function (I\'m using PHP 5.2.17), is there a simple and accurate way to do this? I am thinking of something like the code below, but I don
my function to resolve issue
function diffMonth($from, $to) {
$fromYear = date("Y", strtotime($from));
$fromMonth = date("m", strtotime($from));
$toYear = date("Y", strtotime($to));
$toMonth = date("m", strtotime($to));
if ($fromYear == $toYear) {
return ($toMonth-$fromMonth)+1;
} else {
return (12-$fromMonth)+1+$toMonth;
}
}
Follow up on the answer of @deceze (I've upvoted on his answer). Month will still count as a whole even if the day of the first date didn't reached the day of the second date.
Here's my simple solution on including the day:
$ts1=strtotime($date1);
$ts2=strtotime($date2);
$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);
$month1 = date('m', $ts1);
$month2 = date('m', $ts2);
$day1 = date('d', $ts1); /* I'VE ADDED THE DAY VARIABLE OF DATE1 AND DATE2 */
$day2 = date('d', $ts2);
$diff = (($year2 - $year1) * 12) + ($month2 - $month1);
/* IF THE DAY2 IS LESS THAN DAY1, IT WILL LESSEN THE $diff VALUE BY ONE */
if($day2<$day1){ $diff=$diff-1; }
The logic is, if the day of the second date is less than the day of the first date, it will reduce the value of $diff
variable by one.
$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);
If Month switched from Jan to Feb above code will return you the $diff = 1 But if you want to consider next month only after 30 days then add below lines of code along with above.
$day1 = date('d', $ts1);
$day2 = date('d', $ts2);
if($day2 < $day1){ $diff = $diff - 1; }
This is a simple method I wrote in my class to count the number of months involved into two given dates :
public function nb_mois($date1, $date2)
{
$begin = new DateTime( $date1 );
$end = new DateTime( $date2 );
$end = $end->modify( '+1 month' );
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($begin, $interval, $end);
$counter = 0;
foreach($period as $dt) {
$counter++;
}
return $counter;
}
Here is my solution. It's only checks years and monthes of dates. So, if one date is '31.10.15' and other is '02.11.15' it returns 1 month.
function get_interval_in_month($from, $to) {
$month_in_year = 12;
$date_from = getdate(strtotime($from));
$date_to = getdate(strtotime($to));
return ($date_to['year'] - $date_from['year']) * $month_in_year -
($month_in_year - $date_to['mon']) +
($month_in_year - $date_from['mon']);
}
To calculate the number of CALENDAR MONTHS (as also asked here) between two dates, I usually end up doing something like this. I convert the two dates to strings like "2020-05" and "1994-05", then get their respective result from the below function, then run a subtraction of those results.
/**
* Will return number of months. For 2020 April, that will be the result of (2020*12+4) = 24244
* 2020-12 = 24240 + 12 = 24252
* 2021-01 = 24252 + 01 = 24253
* @param string $year_month Should be "year-month", like "2020-04" etc.
*/
static private function calculate_month_total($year_month)
{
$parts = explode('-', $year_month);
$year = (int)$parts[0];
$month = (int)$parts[1];
return $year * 12 + $month;
}