The basic syntax of the date_add function is as follows:
<span><span><span class="hljs-title function_ invoke__">date_add</span></span><span>(DateTime </span><span><span class="hljs-variable">$object</span></span><span>, DateInterval </span><span><span class="hljs-variable">$interval</span></span><span>) : DateTime
</span></span>
$object: a DateTime object representing the current time.
$interval: a DateInterval object representing the time interval to add.
This function returns a new DateTime object, which is the result after adding the time interval.
Before using date_add, we first need to create a DateTime object. This is usually done with new DateTime(), or by specifying a specific date.
<span><span><span class="hljs-variable">$date</span></span><span> = </span><span><span class="hljs-keyword">new</span></span><span> </span><span><span class="hljs-title class_">DateTime</span></span><span>(</span><span><span class="hljs-string">'2025-06-16 14:00:00'</span></span><span>);
</span></span>
Here, we have created a DateTime object representing June 16, 2025, at 14:00:00.
A DateInterval object represents a time interval. You can create it using a string starting with P, formatted as follows:
P: denotes the period
Followed by time units like Y (years), M (months), D (days), H (hours), I (minutes), and S (seconds).
For example, to represent 1 day, use P1D; to represent 2 hours, use PT2H.
<span><span><span class="hljs-variable">$interval</span></span><span> = </span><span><span class="hljs-keyword">new</span></span><span> </span><span><span class="hljs-title class_">DateInterval</span></span><span>('P1D'); </span><span><span class="hljs-comment">// adds 1 day</span></span><span>
</span></span>
With DateTime and DateInterval objects ready, we can use the date_add function for time calculations. For example, to add 5 days to 2025-06-16 14:00:00:
<span><span><span class="hljs-variable">$date</span></span><span> = </span><span><span class="hljs-keyword">new</span></span><span> </span><span><span class="hljs-title class_">DateTime</span></span><span>('2025-06-16 14:00:00');
</span><span><span class="hljs-variable">$interval</span></span><span> = </span><span><span class="hljs-keyword">new</span></span><span> </span><span><span class="hljs-title class_">DateInterval</span></span><span>('P5D'); </span><span><span class="hljs-comment">// adds 5 days</span></span><span>
</span></span><span><span class="hljs-title function_ invoke__">date_add</span></span><span>($date, $interval);
</span><span><span class="hljs-keyword">echo</span> $date-><span class="hljs-title function_ invoke__">format</span>('Y-m-d H:i:s');
</span>
The output is:
<span><span><span class="hljs-number">2025-06-21 14:00:00</span></span><span>
</span></span>
As shown, date_add added 5 days to the original date, resulting in a new date.
Besides days, we can also add hours and minutes. For example, to add 3 hours and 45 minutes to a given time:
<span><span><span class="hljs-variable">$date</span></span><span> = </span><span><span class="hljs-keyword">new</span></span><span> </span><span><span class="hljs-title class_">DateTime</span></span><span>('2025-06-16 14:00:00');
</span><span><span class="hljs-variable">$interval</span></span><span> = </span><span><span class="hljs-keyword">new</span></span><span> </span><span><span class="hljs-title class_">DateInterval</span></span><span>('PT3H45M'); </span><span><span class="hljs-comment">// adds 3 hours and 45 minutes</span></span><span>
</span></span><span><span class="hljs-title function_ invoke__">date_add</span></span><span>($date, $interval);
</span><span><span class="hljs-keyword">echo</span> $date-><span class="hljs-title function_ invoke__">format</span>('Y-m-d H:i:s');
</span>
The output is:
<span><span><span class="hljs-number">2025-06-16 17:45:00</span></span><span>
</span></span>
You can subtract time by using a negative interval. For example, to subtract 2 days:
<span><span><span class="hljs-variable">$date</span></span><span> = </span><span><span class="hljs-keyword">new</span></span><span> </span><span><span class="hljs-title class_">DateTime</span></span><span>('2025-06-16 14:00:00');
</span><span><span class="hljs-variable">$interval</span></span><span> = </span><span><span class="hljs-keyword">new</span></span><span> </span><span><span class="hljs-title class_">DateInterval</span></span><span>('P2D'); </span><span><span class="hljs-comment">// 2 days</span></span><span>
</span></span><span>$interval->invert = </span><span>1; </span><span><span class="hljs-comment">// invert to negative</span></span><span>
</span></span><span><span class="hljs-title function_ invoke__">date_add</span></span><span>($date, $interval);
</span><span><span class="hljs-keyword">echo</span> $date-><span class="hljs-title function_ invoke__">format</span>('Y-m-d H:i:s');
</span>
The output is:
<span><span><span class="hljs-number">2025-06-14 14:00:00</span></span><span>
</span></span>
By setting the invert property to 1, the DateInterval represents a negative time interval, enabling subtraction.
date_add also supports more complex intervals, such as adding years, months, and days together:
<span><span><span class="hljs-variable">$date</span></span><span> = </span><span><span class="hljs-keyword">new</span></span><span> </span><span><span class="hljs-title class_">DateTime</span></span><span>('2025-06-16 14:00:00');
</span><span><span class="hljs-variable">$interval</span></span><span> = </span><span><span class="hljs-keyword">new</span></span><span> </span><span><span class="hljs-title class_">DateInterval</span></span><span>('P1Y2M5D'); </span><span><span class="hljs-comment">// adds 1 year, 2 months, 5 days</span></span><span>
</span></span><span><span class="hljs-title function_ invoke__">date_add</span></span><span>($date, $interval);
</span><span><span class="hljs-keyword">echo</span> $date-><span class="hljs-title function_ invoke__">format</span>('Y-m-d H:i:s');
</span>
The output is:
<span><span><span class="hljs-number">2026-08-21 14:00:00</span></span><span>
</span></span>
In this example, we added 1 year, 2 months, and 5 days.
date_add is a very useful function in PHP that helps us perform date and time addition and subtraction. By combining DateTime and DateInterval objects, we can flexibly handle various time interval calculations. Whether adding days, hours, minutes, or complex combinations of multiple time units, it can be easily managed. Mastering these date and time operations is essential in real-world development and can greatly improve the efficiency of handling time logic.