3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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;
based on 2bedP
Output for 5.6.0 - 7.3.0alpha1
2011-01-01 >> 2012-05-31 >>>> 2011-01-01 >> 2012-06-01 >>> 17