What is PHP date_add() Function?
If you want to add days, months, years, hours, minutes and seconds to a Date, use PHP date_add() function. Here, as “Date” we use DateTIme object and as “amount of time to add” we use DateInterval object.
Syntax:
date_add(object, interval)
Parameters:
The function has 2 required parameters-
object (Required): It specifies a DateTime object typically created with date_create() function or new DateTIme().
interval (Required): It specifies a DateInterval object which can be created with date_interval_create_from_date_string() function or new DateInterval().
Return Values:
The function returns-
- the modified DateTIme object on success or
- FALSE on failure.
Examples:
Example 1: Adding days to a date
<?php
$datetimeObj = date_create("2026-01-12");
date_add($datetimeObj, date_interval_create_from_date_string("7 days"));
echo "Adding 7 days, the date '2026-01-12' becomes " . date_format($datetimeObj, "Y-m-d");
?>
Output:
Adding 7 days, the date '2026-01-12' becomes 2026-01-19
Example 2: Adding year, month, and days to a date
<?php
$datetimeObj = date_create("2026-01-12");
$interval = date_interval_create_from_date_string('1 year + 6 months + 5 days');
date_add($datetimeObj, $interval);
echo "Adding 1 year + 6 months + 5 days, the date '2026-01-12' becomes " . date_format($datetimeObj, "Y/m/d");
?>
Output:
Adding 1 year + 6 months + 5 days, the date '2026-01-12' becomes 2027/07/17
Example 3: Adding days to a date using DateInterval class (OOP way)
<?php
$datetimeObj = new DateTime('2026-01-12');
$interval = new DateInterval('P15D'); // Here, P = Period, 1D= 1 Day
$datetimeObj->add($interval);
echo "Adding 15 days, the date '2026-01-12' becomes " . $datetimeObj->format('Y-m-d');
?>
Output:
Adding 15 days, the date '2026-01-12' becomes 2026-01-27
Notes on date_add() Function:
It is an alias of the DateTime::add() method.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP date_add() Function
The date_add() function is one of the built-in PHP date and time function. Use this function to add a specific amount of time to a date object.