date_sub

Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object

Syntax

date_sub ( DateTime $object , DateInterval $interval ) : DateTime

Parameters

object

A DateTime object returned by date_create(). The function modifies this object.

interval

A DateInterval object

Return

Returns the DateTime object for method chaining or FALSE on failure.

Examples

object interval

<?

$object = date_create();
$interval = date_interval_create_from_date_string("1 year 1 month 1 week 1 day 1 hour 1 minute 1 second");

date_sub($object, $interval);

echo date_format($object, "Y-m-d H:i:s");

?>
2020-10-29 17:37:47

Return

<?

$object = date_create();
$interval = date_interval_create_from_date_string("1 year 1 month 1 week 1 day 1 hour 1 minute 1 second");

$return = date_sub($object, $interval);

echo date_format($return, "Y-m-d H:i:s");

?>
2020-10-29 17:37:47

PHP

Home Menu