0

i have a problem to get month difference between two dates in months.

$d1 = date_create('January 1, 2013');
$date = date("F j, Y");
$d2 = date_create($date);
$dif = date_diff($d1, $d2);
 //echo $dif->format('%y years');
 echo $dif->format('%m months');

It shows months but not the whole difference in months. I just want the diff in months between dates.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Adeel Gill
  • 353
  • 4
  • 19

1 Answers1

2

%m only shows up to 11 months. After that years are populated. If you want total months you need to figure in years and do some math:

$d1  = date_create('January 1, 2013');
$d2  = date_create();
$dif = date_diff($d1, $d2);
echo ($dif->format('%m') + $dif->format('%y') * 12) . ' months';

Demo

FYI, the above solution removes unnecessary code. If you want today's date you just don't pass any parameters to date_create().

John Conde
  • 217,595
  • 99
  • 455
  • 496