Check the difference between two dates in months

3

I need to put a code in php where I report 2 dates and it returns the difference in months. Example:

 $data1 = "2016-03-01";
 $data2 = "2016-06-10";

 Resultado 3 meses

Example 2:

 $data1 = "2015-06-01";
 $data2 = "2016-06-10";

 Resultado 12 meses

Resolved like this:      $ monthsAhead = $ interval-> m + ($ interval-> 12);

    
asked by anonymous 10.06.2016 / 13:47

1 answer

2

You can do this as follows:

$data1 = new DateTime( '2013-12-11' );
$data2 = new DateTime( '1994-04-17' );

$intervalo = $data1->diff( $data2 );

echo "Intervalo é de {$intervalo->y} anos, {$intervalo->m} meses e {$intervalo->d} dias"; 
    
10.06.2016 / 13:56