PHP calculates the difference between two years of the month, note that I only months, such as "2013-07-03" "2014-03-12", they are 8 months apart, then your function must return 8, pay attention to the code redundancy
Reply content:
PHP calculates the difference between two years of the month, note that I only months, such as "2013-07-03" "2014-03-12", they are 8 months apart, then your function must return 8, pay attention to the code redundancy
The previous answer did not consider the year difference (but the main emphasis on the month only), here to fill @ 2014-03-27 10:06:25:
$time_begin = strtotime("2012-07-03");$time_end = strtotime("2014-03-12");$time_differ = $time_end - $time_begin;$year_differ = date('Y', $time_differ);$month_differ = date('m', $time_differ);$result = 12*intval($year_differ-1970)+intval($month_differ)-1;
$time_begin = strtotime("2013-07-03");$time_end = strtotime("2014-03-12");$time_differ = $time_end - $time_begin;$month_differ = date('m', $time_differ);$result = intval($month_differ)-1;
The pro-test is feasible.
$d1 = new DateTime('2013-07-03'); $d2 = new DateTime('2014-03-12'); $diff=$d2->diff($d1); echo ($diff->y*12)+$diff->m;