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 "{$nowObject->format('Y-m-d')} - $dob = {$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(41) "2014-03-11 - 2014-03-10 = 0-0-0 / days: 0" string(41) "2014-03-11 - 2014-03-09 = 0-0-1 / days: 1" string(41) "2014-03-11 - 2014-03-08 = 0-0-2 / days: 2" string(41) "2014-03-11 - 2014-03-07 = 0-0-3 / days: 3" string(41) "2014-03-11 - 2014-03-06 = 0-0-4 / days: 4" string(41) "2014-03-11 - 2014-03-05 = 0-0-5 / days: 5" string(41) "2014-03-11 - 2014-03-04 = 0-0-6 / days: 6" string(41) "2014-03-11 - 2014-03-03 = 0-0-7 / days: 7" string(41) "2014-03-11 - 2014-03-02 = 0-0-8 / days: 8" string(41) "2014-03-11 - 2014-03-01 = 0-0-9 / days: 9" string(43) "2014-03-11 - 2014-02-28 = 0-0-10 / days: 10" string(43) "2014-03-11 - 2014-02-27 = 0-0-11 / days: 11" string(43) "2014-03-11 - 2014-02-26 = 0-0-12 / days: 12" string(43) "2014-03-11 - 2014-02-25 = 0-0-13 / days: 13" string(43) "2014-03-11 - 2014-02-24 = 0-0-14 / days: 14" string(43) "2014-03-11 - 2014-02-23 = 0-0-15 / days: 15" string(43) "2014-03-11 - 2014-02-22 = 0-0-16 / days: 16" string(43) "2014-03-11 - 2014-02-21 = 0-0-17 / days: 17" string(43) "2014-03-11 - 2014-02-20 = 0-0-18 / days: 18" string(43) "2014-03-11 - 2014-02-19 = 0-0-19 / days: 19" string(43) "2014-03-11 - 2014-02-18 = 0-0-20 / days: 20" string(43) "2014-03-11 - 2014-02-17 = 0-0-21 / days: 21" string(43) "2014-03-11 - 2014-02-16 = 0-0-22 / days: 22" string(43) "2014-03-11 - 2014-02-15 = 0-0-23 / days: 23" string(43) "2014-03-11 - 2014-02-14 = 0-0-24 / days: 24" string(43) "2014-03-11 - 2014-02-13 = 0-0-25 / days: 25" string(43) "2014-03-11 - 2014-02-12 = 0-0-26 / days: 26" string(43) "2014-03-11 - 2014-02-11 = 0-0-27 / days: 27" string(42) "2014-03-11 - 2014-02-10 = 0-1-0 / days: 28" string(42) "2014-03-11 - 2014-02-09 = 0-1-1 / days: 29" string(42) "2014-03-11 - 2014-02-08 = 0-1-2 / days: 30" string(42) "2014-03-11 - 2014-02-07 = 0-1-3 / days: 31" string(42) "2014-03-11 - 2014-02-06 = 0-1-4 / days: 32" string(42) "2014-03-11 - 2014-02-05 = 0-1-5 / days: 33" string(42) "2014-03-11 - 2014-02-04 = 0-1-6 / days: 34" string(42) "2014-03-11 - 2014-02-03 = 0-1-7 / days: 35" string(42) "2014-03-11 - 2014-02-02 = 0-1-8 / days: 36" string(42) "2014-03-11 - 2014-02-01 = 0-1-9 / days: 37" string(43) "2014-03-11 - 2014-01-31 = 0-1-10 / days: 38" string(43) "2014-03-11 - 2014-01-30 = 0-1-11 / days: 39" string(43) "2014-03-11 - 2014-01-29 = 0-1-12 / days: 40" string(43) "2014-03-11 - 2014-01-28 = 0-1-13 / days: 41" string(43) "2014-03-11 - 2014-01-27 = 0-1-14 / days: 42" string(43) "2014-03-11 - 2014-01-26 = 0-1-15 / days: 43" string(43) "2014-03-11 - 2014-01-25 = 0-1-16 / days: 44" string(43) "2014-03-11 - 2014-01-24 = 0-1-17 / days: 45" string(43) "2014-03-11 - 2014-01-23 = 0-1-18 / days: 46" string(43) "2014-03-11 - 2014-01-22 = 0-1-19 / days: 47" string(43) "2014-03-11 - 2014-01-21 = 0-1-20 / days: 48" string(43) "2014-03-11 - 2014-01-20 = 0-1-21 / days: 49" string(43) "2014-03-11 - 2014-01-19 = 0-1-22 / days: 50" string(43) "2014-03-11 - 2014-01-18 = 0-1-23 / days: 51" string(43) "2014-03-11 - 2014-01-17 = 0-1-24 / days: 52" string(43) "2014-03-11 - 2014-01-16 = 0-1-25 / days: 53" string(43) "2014-03-11 - 2014-01-15 = 0-1-26 / days: 54" string(43) "2014-03-11 - 2014-01-14 = 0-1-27 / days: 55" string(43) "2014-03-11 - 2014-01-13 = 0-1-28 / days: 56" string(43) "2014-03-11 - 2014-01-12 = 0-1-29 / days: 57" string(43) "2014-03-11 - 2014-01-11 = 0-1-30 / days: 58" string(42) "2014-03-11 - 2014-01-10 = 0-2-0 / days: 59" string(42) "2014-03-11 - 2014-01-09 = 0-2-1 / days: 60" string(42) "2014-03-11 - 2014-01-08 = 0-2-2 / days: 61" string(42) "2014-03-11 - 2014-01-07 = 0-2-3 / days: 62" string(42) "2014-03-11 - 2014-01-06 = 0-2-4 / days: 63" string(42) "2014-03-11 - 2014-01-05 = 0-2-5 / days: 64" string(42) "2014-03-11 - 2014-01-04 = 0-2-6 / days: 65" string(42) "2014-03-11 - 2014-01-03 = 0-2-7 / days: 66" string(42) "2014-03-11 - 2014-01-02 = 0-2-8 / days: 67" string(42) "2014-03-11 - 2014-01-01 = 0-2-9 / days: 68"
Output for 5.3.0 - 5.3.2
string(41) "2014-03-11 - 2014-03-10 = 0-0-0 / days: 0" string(41) "2014-03-11 - 2014-03-09 = 0-0-1 / days: 1" string(41) "2014-03-11 - 2014-03-08 = 0-0-2 / days: 2" string(41) "2014-03-11 - 2014-03-07 = 0-0-3 / days: 3" string(41) "2014-03-11 - 2014-03-06 = 0-0-4 / days: 4" string(41) "2014-03-11 - 2014-03-05 = 0-0-5 / days: 5" string(41) "2014-03-11 - 2014-03-04 = 0-0-6 / days: 6" string(41) "2014-03-11 - 2014-03-03 = 0-0-7 / days: 7" string(41) "2014-03-11 - 2014-03-02 = 0-0-8 / days: 8" string(41) "2014-03-11 - 2014-03-01 = 0-0-9 / days: 9" string(43) "2014-03-11 - 2014-02-28 = 0-0-10 / days: 10" string(43) "2014-03-11 - 2014-02-27 = 0-0-11 / days: 11" string(43) "2014-03-11 - 2014-02-26 = 0-0-12 / days: 12" string(43) "2014-03-11 - 2014-02-25 = 0-0-13 / days: 13" string(43) "2014-03-11 - 2014-02-24 = 0-0-14 / days: 14" string(43) "2014-03-11 - 2014-02-23 = 0-0-15 / days: 15" string(43) "2014-03-11 - 2014-02-22 = 0-0-16 / days: 16" string(43) "2014-03-11 - 2014-02-21 = 0-0-17 / days: 17" string(43) "2014-03-11 - 2014-02-20 = 0-0-18 / days: 18" string(43) "2014-03-11 - 2014-02-19 = 0-0-19 / days: 19" string(43) "2014-03-11 - 2014-02-18 = 0-0-20 / days: 20" string(43) "2014-03-11 - 2014-02-17 = 0-0-21 / days: 21" string(43) "2014-03-11 - 2014-02-16 = 0-0-22 / days: 22" string(43) "2014-03-11 - 2014-02-15 = 0-0-23 / days: 23" string(43) "2014-03-11 - 2014-02-14 = 0-0-24 / days: 24" string(43) "2014-03-11 - 2014-02-13 = 0-0-25 / days: 25" string(43) "2014-03-11 - 2014-02-12 = 0-0-26 / days: 26" string(43) "2014-03-11 - 2014-02-11 = 0-0-27 / days: 27" string(42) "2014-03-11 - 2014-02-10 = 0-1-0 / days: 28" string(42) "2014-03-11 - 2014-02-09 = 0-1-1 / days: 29" string(42) "2014-03-11 - 2014-02-08 = 0-1-2 / days: 30" string(42) "2014-03-11 - 2014-02-07 = 0-1-3 / days: 31" string(42) "2014-03-11 - 2014-02-06 = 0-1-4 / days: 32" string(42) "2014-03-11 - 2014-02-05 = 0-1-5 / days: 33" string(42) "2014-03-11 - 2014-02-04 = 0-1-6 / days: 34" string(42) "2014-03-11 - 2014-02-03 = 0-1-7 / days: 35" string(42) "2014-03-11 - 2014-02-02 = 0-1-8 / days: 36" string(42) "2014-03-11 - 2014-02-01 = 0-1-9 / days: 37" string(43) "2014-03-11 - 2014-01-31 = 0-1-10 / days: 38" string(43) "2014-03-11 - 2014-01-30 = 0-1-11 / days: 39" string(43) "2014-03-11 - 2014-01-29 = 0-1-12 / days: 40" string(43) "2014-03-11 - 2014-01-28 = 0-1-13 / days: 41" string(43) "2014-03-11 - 2014-01-27 = 0-1-14 / days: 42" string(43) "2014-03-11 - 2014-01-26 = 0-1-15 / days: 43" string(43) "2014-03-11 - 2014-01-25 = 0-1-16 / days: 44" string(43) "2014-03-11 - 2014-01-24 = 0-1-17 / days: 45" string(43) "2014-03-11 - 2014-01-23 = 0-1-18 / days: 46" string(43) "2014-03-11 - 2014-01-22 = 0-1-19 / days: 47" string(43) "2014-03-11 - 2014-01-21 = 0-1-20 / days: 48" string(43) "2014-03-11 - 2014-01-20 = 0-1-21 / days: 49" string(43) "2014-03-11 - 2014-01-19 = 0-1-22 / days: 50" string(43) "2014-03-11 - 2014-01-18 = 0-1-23 / days: 51" string(43) "2014-03-11 - 2014-01-17 = 0-1-24 / days: 52" string(43) "2014-03-11 - 2014-01-16 = 0-1-25 / days: 53" string(43) "2014-03-11 - 2014-01-15 = 0-1-26 / days: 54" string(43) "2014-03-11 - 2014-01-14 = 0-1-27 / days: 55" string(43) "2014-03-11 - 2014-01-13 = 0-1-28 / days: 56" string(42) "2014-03-11 - 2014-01-12 = 0-2-1 / days: 57" string(42) "2014-03-11 - 2014-01-11 = 0-2-2 / days: 58" string(42) "2014-03-11 - 2014-01-10 = 0-2-0 / days: 59" string(42) "2014-03-11 - 2014-01-09 = 0-2-1 / days: 60" string(42) "2014-03-11 - 2014-01-08 = 0-2-2 / days: 61" string(42) "2014-03-11 - 2014-01-07 = 0-2-3 / days: 62" string(42) "2014-03-11 - 2014-01-06 = 0-2-4 / days: 63" string(42) "2014-03-11 - 2014-01-05 = 0-2-5 / days: 64" string(42) "2014-03-11 - 2014-01-04 = 0-2-6 / days: 65" string(42) "2014-03-11 - 2014-01-03 = 0-2-7 / days: 66" string(42) "2014-03-11 - 2014-01-02 = 0-2-8 / days: 67" string(42) "2014-03-11 - 2014-01-01 = 0-2-9 / days: 68"
Output for 5.2.2 - 5.2.17
Fatal error: Call to undefined method DateTime::sub() in /in/32HOM 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/32HOM on line 16
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected '(', expecting '}' in /in/32HOM on line 13
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected '(', expecting '}' in /in/32HOM on line 13
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'}'' in /in/32HOM on line 13
Process exited with code 255.