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 single parameter $interval which holds the number of days or months or years or hours or minutes or seconds which are going to be added to the given DateTimeImmutable object.
Return Values: This function returns the final DateTimeImmutable object after addition is done.
Below programs illustrate the DateTimeImmutable::add() function in PHP:
Program 1: This program uses DateTimeImmutable::add() function to add 2 days to the DateTimeImmutable object.
<?php // PHP program to illustrate DateTimeImmutable::add() // function // Creating a new DateTimeImmutable::add() object $datetime = new DateTimeImmutable("2019-10-03T10:00:00"); // Initializing a date interval of 2 days $interval = 'P2D'; // Calling the add() function $datetime = $datetime->add(new DateInterval($interval)); // Getting a new date time in the // format of 'Y-m-d H:i:s' echo $datetime->format('Y-m-d H:i:s'); ?> |
2019-10-05 10:00:00
Program 2: This program uses DateTimeImmutable::add() function to add ‘P2Y5M2DT0H30M40S’ DateInterval to the DateTimeImmutable object.
<?php // PHP program to illustrate DateTimeImmutable::add() // function // Creating a new DateTimeImmutable::add() object $datetime = new DateTimeImmutable("2019-10-03T10:00:00"); // Initializing a DateInterval object $interval = 'P2Y5M2DT0H30M40S'; // Calling the add() function $datetime = $datetime->add(new DateInterval($interval)); // Getting a new date time in the // format of 'Y-m-d H:i:s' echo $datetime->format('Y-m-d H:i:s'); ?> |
2022-03-05 10:30:40
Reference: https://www.php.net/manual/en/datetimeimmutable.add.php
Recommended Posts:
- PHP | DateTimeImmutable setDate() Function
- PHP | DateTimeImmutable setISODate() Function
- PHP | DateTimeImmutable setTimestamp() Function
- PHP | DateTimeImmutable setTime() Function
- PHP | DateTimeImmutable modify() Function
- PHP | DateTimeImmutable::setTimezone() Function
- PHP | DateTimeImmutable::sub() Function
- PHP | Ds\Set add() Function
- PHP | SplDoublyLinkedList add() Function
- PHP | IntlCalendar add() Function
- PHP | DateTime add() Function
- How to add API function to a simple PHP Page ?
- PHP program to add item at the beginning of associative array
- How to add http:// if it doesn't exists in the URL in PHP?
- How to add days to $Date in PHP?
- How to add 24 hours to a unix timestamp in php?
- How to add a PHP page to WordPress?
- D3.js | d3.set.add() Function
- How to add sleep/wait function before continuing in JavaScript?
- How To Add Google Maps With A Marker to a Website
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

