In PHP, date_add() adds a DateInterval, such as 10 days or 1 month, to a DateTime object. It changes that object and also returns it.
date_add() is the procedural alias of DateTime::add(). Both do exactly the same job. Because the object itself is modified, the original value is lost. The function only accepts DateTime, not DateTimeImmutable, so for modern code many developers prefer DateTimeImmutable::add(), which returns a new object and leaves the original untouched.
Syntax
Call date_add() with the values you need. Start with this line.
PHP
date_add($date, new DateInterval('P10D'));
Call
What it does
date_add($date, new DateInterval('P10D'))
Moves $date forward by 10 days.
date_add($date, new DateInterval('P1M'))
Moves $date forward by one month.
$date->add(new DateInterval('PT12H'))
The object style (DateTime::add) that does the same thing as date_add(), here adding 12 hours.
date_add() is an alias of DateTime::add(). It only works with DateTime. Passing a DateTimeImmutable throws a TypeError, so use $immutable->add() instead. To subtract time, use date_sub() or DateTime::sub().
Required. A DateTime object, usually created with date_create() or new DateTime(). This object is modified.
interval
Required. A DateInterval object, for example new DateInterval('P1D'). The interval string starts with P, and a T separates date parts from time parts, as in 'P1DT2H'.
Return Value
Return Value
Description
DateTime
The same DateTime object that you passed in, after the interval was added. Because it is the same object, the original variable already shows the new value.
PHP Examples
Example 1 — Adding Days, Months and Hours
Create a DateTime from a fixed date and add three different intervals. Each call changes the same object.
date_create('2024-01-15') makes a DateTime for midnight on January 15, 2024.
P10D means a period of 10 days, so the date becomes 2024-01-25.
P1M adds one month to the already changed object, giving 2024-02-25.
PT12H adds 12 hours. The $date->add() form is the same as date_add().
Example 2 — Month Overflow and the Same Object
Adding a month to January 31 does not land on a real February date. PHP rolls the extra days into March. The function also returns the very same object.
PHP
<?php
$a = date_create('2024-01-31');
$b = date_add($a, new DateInterval('P1M'));
echo date_format($a, 'Y-m-d'), PHP_EOL;
var_dump($a === $b);
$c = date_create('2024-01-31');
date_modify($c, 'last day of next month');
echo date_format($c, 'Y-m-d'), PHP_EOL;
Output
2024-03-02
bool(true)
2024-02-29
How It Works
January 31 plus one month would be February 31, which does not exist.
PHP moves the extra days into March, and because 2024 is a leap year the result is 2024-03-02.
$a === $b is true, because date_add() returns the same object it modified.
To stay inside February, use date_modify() with 'last day of next month', which gives 2024-02-29.
Example 3 — DateTimeImmutable Does Not Work with date_add()
The modern DateTimeImmutable class has its own add() method that returns a new object. The procedural date_add() rejects it with a TypeError.
PHP
<?php
$start = new DateTimeImmutable('2024-01-15');
$end = $start->add(new DateInterval('P1W'));
echo $start->format('Y-m-d'), PHP_EOL;
echo $end->format('Y-m-d'), PHP_EOL;
try {
date_add($start, new DateInterval('P1D'));
} catch (TypeError $e) {
echo "date_add() needs a DateTime, not DateTimeImmutable", PHP_EOL;
}
Output
2024-01-15
2024-01-22
date_add() needs a DateTime, not DateTimeImmutable
How It Works
$start->add() on a DateTimeImmutable leaves $start alone and returns a new object.
P1W adds one week, so $end is 2024-01-22 while $start is still 2024-01-15.
date_add() is declared to take a DateTime, so passing a DateTimeImmutable throws a TypeError.
The catch block prints a friendly message. In new code, call ->add() on the immutable object.
Frequently Asked Questions
It adds a DateInterval to a DateTime object. For example, adding P10D moves the date forward by 10 days. The object is changed in place and also returned.
Yes. date_add() is the procedural alias of DateTime::add(). They behave the same, so choose the object style or the function style depending on how the rest of your code is written.
No. date_add() requires a DateTime and throws a TypeError for DateTimeImmutable. Call the add() method on the immutable object instead, and store the returned new object.
Use date_sub() or DateTime::sub() with the same kind of DateInterval. You can also add an interval whose invert property is 1, but date_sub() is clearer.
February does not have a 31st day, so PHP counts the extra days into March. To get the end of the next month instead, use modify('last day of next month').