3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function ageMonths($dob) { //new date time $dob must be Y-m-d format $dobObject = DateTime::createFromFormat('Y-m-d',$dob); //current date $nowObject = new DateTime('2014-03-11'); //difference $diff = $nowObject->diff($dobObject); return "{$diff->y}-{$diff->m}-{$diff->d} / days: {$diff->days}"; } $date = new DateTime('2014-03-11'); while ($date > new DateTime('2014-01-01')) { $date->sub(new DateInterval('P1D')); var_dump(ageMonths($date->format('Y-m-d'))); }
Output for 5.3.3 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
string(15) "0-0-0 / days: 0" string(15) "0-0-1 / days: 1" string(15) "0-0-2 / days: 2" string(15) "0-0-3 / days: 3" string(15) "0-0-4 / days: 4" string(15) "0-0-5 / days: 5" string(15) "0-0-6 / days: 6" string(15) "0-0-7 / days: 7" string(15) "0-0-8 / days: 8" string(15) "0-0-9 / days: 9" string(17) "0-0-10 / days: 10" string(17) "0-0-11 / days: 11" string(17) "0-0-12 / days: 12" string(17) "0-0-13 / days: 13" string(17) "0-0-14 / days: 14" string(17) "0-0-15 / days: 15" string(17) "0-0-16 / days: 16" string(17) "0-0-17 / days: 17" string(17) "0-0-18 / days: 18" string(17) "0-0-19 / days: 19" string(17) "0-0-20 / days: 20" string(17) "0-0-21 / days: 21" string(17) "0-0-22 / days: 22" string(17) "0-0-23 / days: 23" string(17) "0-0-24 / days: 24" string(17) "0-0-25 / days: 25" string(17) "0-0-26 / days: 26" string(17) "0-0-27 / days: 27" string(16) "0-1-0 / days: 28" string(16) "0-1-1 / days: 29" string(16) "0-1-2 / days: 30" string(16) "0-1-3 / days: 31" string(16) "0-1-4 / days: 32" string(16) "0-1-5 / days: 33" string(16) "0-1-6 / days: 34" string(16) "0-1-7 / days: 35" string(16) "0-1-8 / days: 36" string(16) "0-1-9 / days: 37" string(17) "0-1-10 / days: 38" string(17) "0-1-11 / days: 39" string(17) "0-1-12 / days: 40" string(17) "0-1-13 / days: 41" string(17) "0-1-14 / days: 42" string(17) "0-1-15 / days: 43" string(17) "0-1-16 / days: 44" string(17) "0-1-17 / days: 45" string(17) "0-1-18 / days: 46" string(17) "0-1-19 / days: 47" string(17) "0-1-20 / days: 48" string(17) "0-1-21 / days: 49" string(17) "0-1-22 / days: 50" string(17) "0-1-23 / days: 51" string(17) "0-1-24 / days: 52" string(17) "0-1-25 / days: 53" string(17) "0-1-26 / days: 54" string(17) "0-1-27 / days: 55" string(17) "0-1-28 / days: 56" string(17) "0-1-29 / days: 57" string(17) "0-1-30 / days: 58" string(16) "0-2-0 / days: 59" string(16) "0-2-1 / days: 60" string(16) "0-2-2 / days: 61" string(16) "0-2-3 / days: 62" string(16) "0-2-4 / days: 63" string(16) "0-2-5 / days: 64" string(16) "0-2-6 / days: 65" string(16) "0-2-7 / days: 66" string(16) "0-2-8 / days: 67" string(16) "0-2-9 / days: 68"
Output for 5.3.0 - 5.3.2
string(15) "0-0-0 / days: 0" string(15) "0-0-1 / days: 1" string(15) "0-0-2 / days: 2" string(15) "0-0-3 / days: 3" string(15) "0-0-4 / days: 4" string(15) "0-0-5 / days: 5" string(15) "0-0-6 / days: 6" string(15) "0-0-7 / days: 7" string(15) "0-0-8 / days: 8" string(15) "0-0-9 / days: 9" string(17) "0-0-10 / days: 10" string(17) "0-0-11 / days: 11" string(17) "0-0-12 / days: 12" string(17) "0-0-13 / days: 13" string(17) "0-0-14 / days: 14" string(17) "0-0-15 / days: 15" string(17) "0-0-16 / days: 16" string(17) "0-0-17 / days: 17" string(17) "0-0-18 / days: 18" string(17) "0-0-19 / days: 19" string(17) "0-0-20 / days: 20" string(17) "0-0-21 / days: 21" string(17) "0-0-22 / days: 22" string(17) "0-0-23 / days: 23" string(17) "0-0-24 / days: 24" string(17) "0-0-25 / days: 25" string(17) "0-0-26 / days: 26" string(17) "0-0-27 / days: 27" string(16) "0-1-0 / days: 28" string(16) "0-1-1 / days: 29" string(16) "0-1-2 / days: 30" string(16) "0-1-3 / days: 31" string(16) "0-1-4 / days: 32" string(16) "0-1-5 / days: 33" string(16) "0-1-6 / days: 34" string(16) "0-1-7 / days: 35" string(16) "0-1-8 / days: 36" string(16) "0-1-9 / days: 37" string(17) "0-1-10 / days: 38" string(17) "0-1-11 / days: 39" string(17) "0-1-12 / days: 40" string(17) "0-1-13 / days: 41" string(17) "0-1-14 / days: 42" string(17) "0-1-15 / days: 43" string(17) "0-1-16 / days: 44" string(17) "0-1-17 / days: 45" string(17) "0-1-18 / days: 46" string(17) "0-1-19 / days: 47" string(17) "0-1-20 / days: 48" string(17) "0-1-21 / days: 49" string(17) "0-1-22 / days: 50" string(17) "0-1-23 / days: 51" string(17) "0-1-24 / days: 52" string(17) "0-1-25 / days: 53" string(17) "0-1-26 / days: 54" string(17) "0-1-27 / days: 55" string(17) "0-1-28 / days: 56" string(16) "0-2-1 / days: 57" string(16) "0-2-2 / days: 58" string(16) "0-2-0 / days: 59" string(16) "0-2-1 / days: 60" string(16) "0-2-2 / days: 61" string(16) "0-2-3 / days: 62" string(16) "0-2-4 / days: 63" string(16) "0-2-5 / days: 64" string(16) "0-2-6 / days: 65" string(16) "0-2-7 / days: 66" string(16) "0-2-8 / days: 67" string(16) "0-2-9 / days: 68"
Output for 5.2.2 - 5.2.17
Fatal error: Call to undefined method DateTime::sub() in /in/A52t0 on line 19
Process exited with code 255.
Output for 5.2.0 - 5.2.1
Output for 5.0.0 - 5.1.6
Fatal error: Class 'DateTime' not found in /in/A52t0 on line 16
Process exited with code 255.
Output for 4.3.2 - 4.4.9
Fatal error: Cannot instantiate non-existent class: datetime in /in/A52t0 on line 16
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Cannot instantiate non-existent class: datetime in /in/A52t0 on line 16