3v4l.org

run code in 300+ PHP versions simultaneously
<?php //Enter your code here, enjoy! $fechaMIN = "2011-01-01"; $fechaMAX = "2012-05-31"; $ts1 = strtotime( $fechaMIN ); $ts2 = strtotime( $fechaMAX ); $year1 = date( 'Y', $ts1 ); $year2 = date( 'Y', $ts2 ); $month1 = date( 'm', $ts1 ); $month2 = date( 'm', $ts2 ); $day1 = date( 'd', $ts1 ); $day2 = date( 'd', $ts2 ); $month1_days = date( 't', $ts1 ); $month2_days = date( 't', $ts2 ); $d1 = new DateTime( date( 'Y-m-d', $ts1 ) ); $d2 = new DateTime( date( 'Y-m-d', strtotime( $fechaMAX .' +1 day') ) ); $diff = $d2->diff( $d1, true ); $month2_days = (int) date( 't ', $d2->getTimestamp() ); echo "$fechaMIN >> $fechaMAX >>>> " . date( 'Y-m-d ', $d1->getTimestamp() ) . ' >> ' . date( 'Y-m-d ', $d2->getTimestamp() ) ; $diff_months = round( $diff->y * 12 + $diff->m + ( ( $diff->d ) / $month2_days ), 6 ); echo " >>> ". $diff_months .PHP_EOL;

preferences:
57.9 ms | 402 KiB | 5 Q