date_sub()
EspressoCat5 total contributions
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 theDateTime
object returned bydate_create()
.$interval
: This parameter specifies theDateInterval
object 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:
Looking to contribute?
- 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.