$date = new DateTime('2020-01-01'); $date->modify('+1 day'); // add one day echo $date->format('Y-m-d'); // output: 2020-01-02 $date->modify('-2 months'); // subtract two months echo $date->format('Y-m-d'); // output: 2019-11-02
$date = new DateTime('2020-01-01 09:30:00'); $date->modify('4:15pm'); // set time to 4:15pm echo $date->format('Y-m-d H:i:s'); // output: 2020-01-01 16:15:00To use DateTime modify function, no separate package library is required. It is part of the PHP core library.