PHP | DateTime add() Function Last Updated : 10 Oct, 2019 Comments Improve Suggest changes Like Article Like Report The DateTime::add() function is an inbuilt function in PHP which is used to add an amount of time (days, months, years, hours, minutes and seconds) to the given DateTime object. Syntax: Object oriented style: DateTime DateTime::add( DateInterval $interval ) Procedural style: DateTime date_add( DateTime $object, DateInterval $interval ) Parameters: This function uses two parameters as mentioned above and described below: $object: It specifies the DateTime object returned by date_create() function. This function returns a new DateTime object. $interval: This parameter holds the DateInterval object. Return Value: This function returns the new DateTime object after changing on success or False on failure. Below programs illustrate the DateTime::add() function in PHP: Program 1: php <?php // Initialising a DateTime $datetime = new DateTime('2019-09-30'); // DateInterval object is taken as the // parameter of the add() function // Here 1 day is added $datetime->add(new DateInterval('P1D')); // Getting the new date after addition echo $datetime->format('Y-m-d') . "\n"; ?> Output: 2019-10-01 Program 2: php <?php // Initialising a DateTime $datetime = new DateTime('2019-09-30'); // DateInterval object is taken as the // parameter of the add() function // Here 5 hours, 3 Minutes and 10 seconds is added $datetime->add(new DateInterval('PT5H3M10S')); // Getting the new date after addition echo $datetime->format('Y-m-d H:i:s') . "\n"; ?> Output: 2019-09-30 05:03:10 Reference: https://www.php.net/manual/en/datetime.add.php Comment More infoAdvertise with us Next Article PHP | DateTime add() Function K Kanchan_Ray Follow Improve Article Tags : Web Technologies PHP PHP-date-time PHP-function Similar Reads PHP | DateTimeImmutable add() Function The DateTimeImmutable::add() function is an inbuilt function in PHP which is used to add a number of days, months, years, hours, minutes and seconds to a created DateTimeImmutable object. Syntax: DateTimeImmutable DateTimeImmutable::add( DateInterval $interval ) Parameters: This function accepts a s 2 min read PHP | DateTime sub() Function The DateTime::sub() function is an inbuilt function in PHP which is used to subtract a number of days, months, years, hours, minutes and seconds from a created DateTime object. Syntax: Object oriented style: DateTime DateTime::sub( DateInterval interval ) Procedural style: DateTime date_sub( DateTim 2 min read PHP | DateTime diff() Function The DateTime::diff() function is an inbuilt function in PHP which is used to return the difference between two given DateTime objects. Syntax: Object oriented style: DateInterval DateTime::diff( DateTimeInterface $datetime2, bool $absolute = FALSE ) or DateInterval DateTimeImmutable::diff( DateTimeI 2 min read PHP | DateTime modify() Function The DateTime::modify() function is an inbuilt function in PHP which is used to modify or can alter the timestamp of a DateTime object. Syntax: Object oriented style: DateTime DateTime::modify( string $modify ) Procedural style: DateTime date_modify( DateTime $object, string $modify ) Parameters: Thi 2 min read PHP | DateTime format() Function The DateTime::format() function is an inbuilt function in PHP which is used to return the new formatted date according to the specified format. Syntax: Object oriented style string DateTime::format( string $format ) or string DateTimeImmutable::format( string $format ) or string DateTimeInterface::f 1 min read PHP | DateTime setDate() Function The DateTime::setDate() function is an inbuilt function in PHP which is used to reset the current date of DateTime object with the given date-time object. Syntax: Object oriented style: DateTime DateTime::setDate( int $year, int $month, int $day ) Procedural style: DateTime date_date_set( DateTime $ 2 min read PHP | easter_date() Function The easter_date() function is a built-in function in PHP which returns the Easter date in the year passed as an argument. The current year is taken as default year when no arguments are passed as parameter. Syntax: easter_date( $year ) Parameter: The function accepts one optional parameter $year whi 2 min read PHP | date_diff() Function The date_diff() is an inbuilt function in PHP which is used to calculate the difference between two dates. This function returns a DateInterval object on the success and returns FALSE on failure. Syntax: date_diff($datetime1, $datetime2); Parameters: The date_diff() function accepts two parameters a 2 min read PHP | filemtime( ) Function The filemtime() function in PHP is an inbuilt function which is used to return the last time of a specified file when its content was modified. The filemtime() function returns the last time the file was changed as a Unix Timestamp on success and False on failure. The filename is passed as a paramet 2 min read PHP | date_date_set() Function The date_date_set() function is an inbuilt function in PHP which is used to set a new Date. This function has four parameters $object, $year, $month and $day and returns DateTime object on success or false on failure. The date_date_set() function is an alias of DateTime::setDate() function. Syntax: 2 min read Like