3v4l.org

run code in 300+ PHP versions simultaneously
<?php $ts = strtotime('2010-01-31 +1 month'); echo date('Y-m-d', $ts),"\n"; $dt = new \DateTime('2010-01-31'); $dt->add(new \DateInterval('P1M')); echo $dt->format('Y-m-d'),"\n"; $ts = strtotime('2010-01-05 +1 month'); echo date('Y-m-d', $ts),"\n"; $dt = new \DateTime('2010-01-05'); $dt->add(new \DateInterval('P1M')); echo $dt->format('Y-m-d'),"\n"; $orig = strtotime('2010-01-31'); $ts1 = strtotime('+1 month', $orig); $ts2 = strtotime('first day of next month', $orig); $ts3 = strtotime('last day of this month', $ts2); $ts = $ts1; if ($ts1 > $ts3) { $ts = $ts3; } echo date('Y-m-d', $ts),"\n"; $orig = new \DateTime('2010-01-31'); $dt1 = clone $orig; $dt1->add(new \DateInterval('P1M')); $dt2 = clone $orig; $dt2->modify('last day of next month'); $dt = $dt1; if ($dt1 > $dt2) { $dt = $dt2; } echo $dt->format('Y-m-d'),"\n";

preferences:
39.96 ms | 402 KiB | 5 Q