How to Subtract Days from Date in PHP?
This example shows how to subtract days from a given date using the strtotime()
function in PHP.
Syntax
$new_date = date('Y-m-d', strtotime($given_date. ' - N days'));
Here is an example of subtracting 5 days from January 2, 2024
Example
<?php $given_date = "2024-01-02"; $days_to_subtract = 5; $new_date = date('Y-m-d', strtotime($given_date. " - {$days_to_subtract} days")); echo $new_date; ?>
Output
2023-12-28
In this example, the strtotime()
function is used to convert the given date into a Unix timestamp, and then subtract the specified days. The result is formatted using the date()
function with the 'Y-m-d' format.