The date_add Function
The date_sub
function allows adding an interval to a date. It accepts two parameters: a date object created via date_create
and an interval created via date_interval_create_from_date_string
.
Syntax
date_add(DateTime $object, DateInterval $interval): DateTime
Example
Add 5 days to the current date:
<?php
$date = date_create('2023-01-01');
$interval = date_interval_create_from_date_string('5 days');
date_add($date, $interval);
echo date_format($date, 'Y-m-d');
?>
Code execution result:
'2023-01-06'
Example
Add 1 month and 10 days to the specified date:
<?php
$date = date_create('2023-03-15');
$interval = date_interval_create_from_date_string('1 month + 10 days');
date_add($date, $interval);
echo date_format($date, 'Y-m-d');
?>
Code execution result:
'2023-04-25'
See Also
-
the
date_create
function,
which creates a date object -
the
date_sub
function,
which subtracts an interval from a date -
the
date_diff
function,
which calculates the difference between dates