3v4l.org

run code in 500+ PHP versions simultaneously
<?php function diffInMonth(DateTime $start,DateTime $end): int { list($yearStart,$monthStart,$dayStart) = explode(" ",$start->format("Y m dHis")); list($yearEnd,$monthEnd, $dayEnd) = explode(" ",$end->format("Y m dHis")); $mothDiff = ($yearEnd - $yearStart) * 12 + $monthEnd - $monthStart; if($dayStart > $dayEnd) --$mothDiff; return $mothDiff; } $startDate = new DateTime('2022-11-01 00:00:00.0', new DateTimeZone('Africa/Nairobi')); $endDate = new DateTime('2022-12-01 00:00:00.0', new DateTimeZone('Africa/Nairobi')); $month = diffInMonth($startDate, $endDate); echo $month;
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.21, 8.5.0 - 8.5.7
1

preferences:
96.2 ms | 1246 KiB | 4 Q