$date1=new DateTime('2018-04-23'); // date object created.
$date1->add(new DateInterval('P1Y3M')); // interval of 1 year 3 months added
echo $date1->format('Y-M-d'); // Output is 2019-Jul-23
/// by using DateTimeImmutable ///
$date1=new DateTimeImmutable('2018-04-23'); // date object created .
$date1->add(new DateInterval('P1Y3M')); // interval of 1 year 3 months added
echo $date1->format('Y-M-d'); // Output is 2018-Apr-23
In case of date object created using DateTimeImmutable after adding the interval the original value never changes.
$date = new DateTimeImmutable('now', new DateTimeZone('America/New_York'));
echo $date->format('Y-m-d H:i:s');
$date = new DateTimeImmutable('2023-12-25');
$newDate = $date->modify('+1 day');
echo $date->format('Y-m-d'); // Output: 2023-12-25
echo $newDate->format('Y-m-d'); // Output: 2023-12-26
$date = new DateTimeImmutable('now');
$newDate = $date->modify('+1 day');
echo $date->format('Y-m-d : H:i:s').'<br>';
echo $newDate->format('Y-m-d : H:i:s');
Output
2026-09-09 : 16:55:04
2026-09-10 : 16:55:04
Using setDate()
$today = new DateTimeImmutable();
echo $today->format('Y-m-d ').'<br>';
$today->setDate(13,11,2025);
echo $today->format('Y-m-d');
Output
2026-09-09
2026-09-09
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.