3v4l.org

run code in 300+ PHP versions simultaneously
<?php $today = DateTime::createFromFormat('d.m.Y', '17.06.2019'); $dates = [ '17.06.2016', '19.06.2016', '15.06.2016', ]; foreach ($dates as $date) { $mpsAgeT = DateTime::createFromFormat('d.m.Y', $date); // Find year difference $years = $today->format('Y') - $mpsAgeT->format('Y'); // Add to original date (sets it to the same date of the current year) // Only add if the date has passed if ($today > $mpsAgeT->modify('+' . $years . ' years')) { $years++; } /* 17.06.2019 and 17.06.2016 - should returns me 3 17.06.2019 and 19.06.2016 - should returns me 3 17.06.2019 and 15.06.2016 - should returns me 4 */ print $date . " -> " . $years . "\n"; }
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
17.06.2016 -> 3 19.06.2016 -> 3 15.06.2016 -> 4

preferences:
179.29 ms | 407 KiB | 5 Q