$date = new DateTime('2022-12-31'); $date->setDate(2023, 1, 1); echo $date->format('Y-m-d'); // Output: 2023-01-01
$date = new DateTime(); $date->setDate(2022, 6, 21); echo $date->format('Y-m-d'); // Output: 2022-06-21
$date = new DateTime('tomorrow'); $date->setDate(2023, 1, 1); echo $date->format('Y-m-d'); // Output: 2023-01-01This example sets the date to January 1, 2023, even though the original date was "tomorrow". Package/Library: This method is part of the PHP DateTime class which is included in the PHP language.