In this article, you will learn how to subtract a date from another date. You should not confuse this concept with the date_diff() function because the date_diff() function returns the difference between two dates. The date_sub() function subtracts some day, months, years, hours, minutes, or seconds from the date.
What is the syntax of the date_sub() function in PHP?
date_sub(object, interval)
Parameter | Description |
---|---|
object | The DateTime object of the date_create() format – Required |
interval | DateInterval Object – Required |
Examples of the date_sub() function
Example 1. In the following example, we subtract 10 days from the date created using the date_create() function.
<?php
$date=date_create("2014-02-05");
date_sub($date,date_interval_create_from_date_string("10 days"));
echo date_format($date,"d-m-y");
?>