PHP date_sub()
Published Jul 15, 2023
Contribute to Docs
The date_sub() function subtracts a number of days, months, years, hours, minutes and seconds from a given date.
Syntax
date_sub($object, $interval)
The date_sub() function has two required parameters:
$object: This parameter specifies theDateTimeobject returned bydate_create().$interval: This parameter specifies theDateIntervalobject to be subtracted.
The date_sub() function returns a DateTime object after subtracting the interval.
Example
The following example uses the date_sub() function to subtract 30 days from the passed date:
<?php$date = date_create("2023-03-03");date_sub($date, date_interval_create_from_date_string("30 days"));echo date_format($date, "Y-m-d");?>
The example will result in the following output:
2023-02-01
Codebyte Example
This example is runnable and uses the date_sub() function:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PHP on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours