How to set the time portion of a DateTime object to midnight (00:00:00) in PHP?

You can use the setTime() method along with the value 0 for hours, minutes, and seconds to set the time portion of a DateTime object to midnight:

$date = new DateTime(); $date->setTime(0, 0, 0); echo $date->format('Y-m-d H:i:s'); // Output: 2021-11-20 00:00:00

In the example above, a new DateTime object is created with the current date and time. Then, the setTime() method is used to set the hours, minutes, and seconds to 0. Finally, the format() method is used to display the new value of the DateTime object, which should be midnight.