When working with PHP, you may need to add a DateInterval to a mutable DateTime. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
date_add: The supplied DateTime object is modified in place.
What Is date_add in PHP?
Add a DateInterval to a mutable DateTime. The supplied DateTime object is modified in place.
Syntax:
date_add(DateTime $object, DateInterval $interval): DateTime
Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.
Important: A calendar month is not always 30 days. Clone a mutable DateTime before modifications that must preserve the original.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
$date = date_create("2026-01-15", new DateTimeZone("UTC"));
date_add($date, new DateInterval("P7D"));
echo $date->format("Y-m-d");
Output:
2026-01-22
The code focuses on the main operation. The supplied DateTime object is modified in place.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
$date = date_create("2026-01-15", new DateTimeZone("UTC"));
date_add($date, new DateInterval("P1M"));
echo $date->format("Y-m-d");
Output:
2026-02-15
What changes? A calendar month is not always 30 days. Clone a mutable DateTime before modifications that must preserve the original.
Practical Example: Applying It in an Application
Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.
<?php
$dueDate = date_create("2026-06-01", new DateTimeZone("UTC"));
foreach (["P1D", "P2D", "P3D"] as $intervalSpec) {
$nextDueDate = clone $dueDate;
date_add($nextDueDate, new DateInterval($intervalSpec));
echo $nextDueDate->format("Y-m-d") . PHP_EOL;
}
Output:
2026-06-02
2026-06-03
2026-06-04
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. The supplied DateTime object is modified in place.
Important Behavior and Edge Cases
- Core behavior: The supplied DateTime object is modified in place.
- Inputs and results: A calendar month is not always 30 days. Clone a mutable DateTime before modifications that must preserve the original.
- Alternatives: DateTimeImmutable::add() returns a new immutable result.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
date_add | Add a dateinterval to a mutable datetime. |
| Related PHP approach | DateTimeImmutable::add() returns a new immutable result. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | A calendar month is not always 30 days. Clone a mutable DateTime before modifications that must preserve the original. |
| Confusing this API with a related operation | DateTimeImmutable::add() returns a new immutable result. |
| Assuming output is identical across environments | Check return values and required extensions or versions. Use explicit locale, timezone or encoding settings when they affect the result. |
Key Takeaways
- date_add helps you add a DateInterval to a mutable DateTime.
- The supplied DateTime object is modified in place.
- A calendar month is not always 30 days. Clone a mutable DateTime before modifications that must preserve the original.
Use date_add when you need to add a DateInterval to a mutable DateTime. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.