PHP date_sub() function

PHP date_parse_from_format() function
PHP date_sun_info() function

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)
ParameterDescription
objectThe DateTime object of the date_create() format – Required
intervalDateInterval Object – Required
PHP date_sub() method

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");
?>
PHP date_parse_from_format() function
PHP date_sun_info() function

Stay up-to-date about PHP!

We don’t spam!

en English
X
Scroll to Top