$date = new DateTime(); echo $date->format('Y-m-d H:i:s');
$date = new DateTime('2021-01-01'); $date->add(new DateInterval('P1D')); echo $date->format('Y-m-d');
$date1 = new DateTime('2021-01-01'); $date2 = new DateTime('2021-02-01'); $interval = $date1->diff($date2); echo $interval->days;This example creates two new DateTime objects with the dates `2021-01-01` and `2021-02-01`, respectively. Then, it calculates the difference between these two dates using the `diff()` method, and stores the result in an interval object. Finally, the number of days between the two dates is printed to the screen. This example is part of the PHP core library.