<?php
function nextMonth($date, $increment)
{
$1stOfDateMonth = \DateTime::createFromFormat('Y-m-01', $date);
$31stOfDateMonth = \DateTime::createFromFormat('Y-m-t', $date)->format('t');
$31stOfNextMonth = clone $1stOfDateMonth;
$31stOfNextMonth->modify('+' (int) $increment . ' month')->format('t');
$dayOfDateMonth = \DateTime::createFromFormat('Y-m-d', $date)->format('d');
$dt = \DateTime::createFromFormat('Y-m-d', $date);
$dt->modify('+' (int) $increment . ' month');
if ($31stOfNextMonth < $31stOfDateMonth && $31stOfNextMonth < $dayOfDateMonth) {
$dt = \DateTime::createFromFormat('Y-m-01', $date);
$dt->modify('+' (int) $increment . ' month +' . $31stOfNextMonth);
}
return $dt->format('Y-m-d');
}
echo nextMonth('2009-12-31', 2),"\n";
echo nextMonth('2010-01-31', 1),"\n";
echo nextMonth('2010-01-05', 1),"\n";
echo nextMonth('2010-12-05', 1),"\n";
- Output for 5.4.0 - 5.4.22
- Parse error: syntax error, unexpected '1' (T_LNUMBER), expecting variable (T_VARIABLE) or '$' in /in/3QmIi on line 5
Process exited with code 255. - Output for 5.3.0 - 5.3.27
- Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /in/3QmIi on line 5
Process exited with code 255.
preferences:
187.58 ms | 935 KiB | 59 Q