Anonymous View

3v4l.org

run code in 500+ PHP versions simultaneously
<?php function add_calendar_months($date_str, $months){ $dt = new DateTime($date_str, new DateTimeZone('UTC')); $day = (int) $dt->format('j'); $dt->modify('first day of this month'); $dt->modify("+{$months} months"); $max_day = (int) $dt->format('t'); $dt->setDate((int) $dt->format('Y'), (int) $dt->format('n'), min($day, $max_day)); return $dt; } $r = add_calendar_months('2026-02-28 23:59:59', 1); echo "RESULT_TIME=" . $r->format('Y-m-d H:i:s') . "\n"; // also show what 'first day of this month' alone does to time: $d2 = new DateTime('2026-02-28 23:59:59', new DateTimeZone('UTC')); $d2->modify('first day of this month'); echo "FIRSTDAYONLY=" . $d2->format('Y-m-d H:i:s') . "\n";
Output for 8.2.31, 8.3.0 - 8.3.31, 8.4.1 - 8.4.22, 8.5.0 - 8.5.7
RESULT_TIME=2026-03-28 23:59:59 FIRSTDAYONLY=2026-02-01 23:59:59

preferences:
41.52 ms | 750 KiB | 4 Q