The date_add() function is used to add a time interval to a DateTime object. It accepts two parameters: a DateTime object and a DateInterval object representing the time interval. The interval can be a combination of years, months, days, hours, minutes, and other units.
Syntax:
<span><span><span class="hljs-title function_ invoke__">date_add</span></span><span>(DateTime </span><span><span class="hljs-variable">$datetime</span></span><span>, DateInterval </span><span><span class="hljs-variable">$interval</span></span><span>): DateTime
</span></span>
Parameters:
$datetime: The DateTime object to which the interval will be added.
$interval: A DateInterval object representing the time interval to add.
Example:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-variable">$datetime</span></span> = </span><span><span class="hljs-keyword">new</span></span> </span><span><span class="hljs-title class_">DateTime</span></span>('2025-01-01');
</span><span><span class="hljs-variable">$interval</span></span> = </span><span><span class="hljs-keyword">new</span></span> </span><span><span class="hljs-title class_">DateInterval</span></span>('P1D'); </span><span><span class="hljs-comment">// Add 1 day</span></span>
</span><span><span class="hljs-title function_ invoke__">date_add</span></span>($datetime, $interval);
</span><span><span class="hljs-keyword">echo</span> $datetime-><span class="hljs-title function_ invoke__">format</span>('Y-m-d'); </span><span><span class="hljs-comment">// Output: 2025-01-02</span></span>
</span><span><span class="hljs-meta">?></span></span>
</span>
In the code above, we create a DateTime object initialized to '2025-01-01', then create a DateInterval object representing “add 1 day,” and finally use date_add() to add the interval to the date.
Opposite to date_add(), the date_sub() function subtracts a time interval from a DateTime object. Its functionality is almost identical to date_add() but reduces time instead of increasing it.
Syntax:
<span><span><span class="hljs-title function_ invoke__">date_sub</span></span><span>(DateTime </span><span><span class="hljs-variable">$datetime</span></span>, DateInterval </span><span><span class="hljs-variable">$interval</span></span><span>): DateTime
</span></span>
Parameters:
$datetime: The DateTime object from which the interval will be subtracted.
$interval: A DateInterval object representing the time interval to subtract.
Example:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span>$datetime = new DateTime('2025-01-01');
$interval = new DateInterval('P1D'); <span><span class="hljs-comment">// Subtract 1 day</span></span>
</span><span>date_sub($datetime, $interval);
echo $datetime->format('Y-m-d'); <span><span class="hljs-comment">// Output: 2024-12-31</span></span>
</span><span><span class="hljs-meta">?></span></span>
</span>
In this example, we create a DateTime object initialized to '2025-01-01', then a DateInterval object representing “subtract 1 day,” and use date_sub() to subtract the interval from the date.
Functionality: date_add() adds a time interval to a date, while date_sub() subtracts a time interval from a date.
Parameter order: The parameter order and usage are essentially the same for both functions, but the effects are opposite. date_add() performs addition, while date_sub() performs subtraction.
Sometimes, when handling complex date calculations, you may need to use both date_add() and date_sub() to perform multiple operations on a single date. For example, a date might first need two months added and then a week subtracted. You can combine these two functions to achieve that.
Example:
<span><span><span class="hljs-meta"><?php</span></span><span>
$datetime = new DateTime('2025-01-01');
<p></span>// Step 1: Add 2 months<br>
$interval_add = new DateInterval('P2M');<br>
date_add($datetime, $interval_add);</p>
<p></span>// Step 2: Subtract 1 week<br>
$interval_sub = new DateInterval('P1W');<br>
date_sub($datetime, $interval_sub);</p>
<p>echo $datetime->format('Y-m-d'); // Output: 2025-03-04<br>
</span>?><br>
</span>
In this example, we first add two months using date_add() and then subtract a week from the result using date_sub(). The final date is '2025-03-04'.
Calculating future or past dates: You can use date_add() and date_sub() to compute dates in the future or past by performing addition or subtraction.
Handling date and time differences: These functions are useful for calculating the difference between a given date and the current date. By first adjusting the date with date_add() or date_sub() and then comparing, complex date calculations can be managed effectively.
Scheduling and automation: In automated tasks, you may need to adjust task execution times based on specific intervals. date_add() and date_sub() allow you to flexibly set different time points for each task.