English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP Date & Time Functions Manual
The date_add() function adds a certain amount of days, months, years, hours, minutes, and seconds to a DateTime object.
The date_add() function is an alias of DateTime::add(). It takes a DateTime object as a parameter and a DateInterval object, and adds the specified interval to the given DateTime.
date_add($object, $interval)
Serial number | Parameters and descriptions |
---|---|
1 | object (optional) This is a DateTime object used to specify/represents the date to which you need to add the time interval. |
2 | interval (optional) This is a DateInterval object, specifying the interval to be added. |
The PHP date_add() function returns a DateTime object with the added interval. If it fails, this function will return a boolean valuefalse.
This function was initially introduced in PHP version5.3introduced in version 5.2.0 and is available in all higher versions.
The following example demonstratesdate_add()Usage of the function-
<?php //Create a DateTime object $date = date_create("25-09-1989); //Adding interval to the date $res = date_add($date, new DateInterval('PT10H30S')); //Format the date and print it $format = date_format( $res, "d-m-Y H:i:s"); print($format); ?>Test to see‹/›
Output result
25-09-1989 10:00:30
You can usedate_interval_create_from_date_string()The function creates an interval. The following example uses this function to create an interval and add it to a date:
<?php $date = date_create("2019-09-09); $interval = date_interval_create_from_date_string('30 days'); $res = date_add($date, $interval); $format = date_format( $res, "Y-m-d H:i:s"); print($format); ?>Test to see‹/›
Output result
2019-10-09 00:00:00
Now, let's try to add an interval with year, month, and day-
<?php //Create a DateTime object $date = date_create("25-09-1989); //Add time interval to the date $res = date_add($date, new DateInterval('P29Y2M5D')); //Format the date and print it $format = date_format( $res, "Y-m-d"); print($format); ?>Test to see‹/›
Output result
2018-11-30
on the date1995-05-07is added to150 days:
<?php $date = date_create('1995-05-07); $interval = date_interval_create_from_date_string('150 days'); $date->add($interval); >print($date -> format('Y-m-d')); ?>Test to see‹/›
This results in the following-
1995-10-04