Home >>PHP Date Time Functions >PHP date_sub() Function

PHP date_sub() Function

PHP date_sub() Function

The date_sub() function is used to subtracts some days, months, years, hours, minutes, and seconds from a date. The function returns a DateTime object on success otherwise,it returns FALSE on failure.

Syntax:

date_sub(object, interval)

Parameter Values

Parameter Description
object This is required parameter. This parameter specifies a DateTime object returned by date_create().
interval This is required parameter. This parameter specifies a DateInterval object.

Here is an example of date_sub() Function in PHP:

<html>
<body>
<?php
$date=date_create("2014-03-16");
date_sub($date,date_interval_create_from_date_string("40 days"));
echo date_format($date,"Y-m-d");
?>
</body>
</html>
Output:
2014-02-04

Exmaple2:

<html>
<body>
<?php
$date=date_create("2017-05-15");
date_sub($date,date_interval_create_from_date_string("40 days"));
echo date_format($date,"Y-m-d");
?>
</body>
</html>
Output:
2017-04-05

No Sidebar ads