The date_sub Function
The date_sub
function subtracts a time interval from a date object.
The first parameter accepts a DateTime
object, the second - a DateInterval
object.
Syntax
date_sub(DateTime $object, DateInterval $interval);
Example
Subtract 10
days from the specified date:
<?php
$date = new DateTime('2025-06-15');
$interval = new DateInterval('P10D');
date_sub($date, $interval);
echo $date->format('Y-m-d');
?>
Code execution result:
'2025-06-05'
Example
Subtract 1
year and 2
months from the date:
<?php
$date = new DateTime('2025-03-20');
$interval = new DateInterval('P1Y2M');
date_sub($date, $interval);
echo $date->format(
Y-m-d');
?>
Code execution result:
'2024-01-20'
Example
The date_create
function returns a DateTime
object.
You can rewrite the code in a mixed style,
partially OOP and partially functional:
<?php
$date = date_create('2025-03-20');
$interval = new DateInterval('P1Y2M');
date_sub($date, $interval);
echo date_format($date, 'Y-m-d');
?>
Code execution result:
'2024-01-20'