WE will learn how to add date time intervals to a date object. We must understand date interval and date format before using add function to the date object.
Output is here ( Refresh this page to see the change in output)
29-05-22 09:58:54 09-03-23 14:04:06
Using DateTimeImmutable object
DateTimeImmutable never modifies itself but returns a new object instead
$date1=new DateTime('2019-05-30'); // date object created.
$date1->add(new DateInterval('P1Y3M')); // inerval of 1 year 3 months added
echo $date1->format('Y-M-d'); // Output is 2020-Aug-30
/// by using DateTimeImmutable ///
$date1=new DateTimeImmutable('2019-05-30'); // date object created .
$date1->add(new DateInterval('P1Y3M')); // inerval of 1 year 3 months added
echo $date1->format('Y-M-d'); // Output is 2019-May-30
In case of date object created using DateTimeImmutable after adding the interval the original value never changes.
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com