Date::addMonth
Add months to a date
Description
public string Date::addMonth( string $dateTime, int $interval, [ string $format = 'Y-m-d H:i:s' ] )
Returns $dateTime plus $interval months, formatted with date() format $format. When the day does not exist in the target month, the last day of that month is used. The time is reset to 00:00:00.
Arguments
| Name | Data type | Default | Remarks |
|---|---|---|---|
| $dateTime | string | Base date | |
| $interval | int | Number of months (negative: before) | |
| $format | string | 'Y-m-d H:i:s' | date() format of the result |
Example
Controller
use Fegg\Tool\Date;
$date = new Date();
echo $date->addMonth('2024-01-31', 1); // -> "2024-02-29 00:00:00"
echo $date->addMonth('2024-04-15', -6, 'Y/m/d'); // -> "2023/10/15"