How does the DateTime class in PHP handle date conversions from DD.MM.YYYY format to YYYY-MM-DD format?

When converting a date from DD.MM.YYYY format to YYYY-MM-DD format using the DateTime class in PHP, you can use the DateTime::createFromFormat() method to specify the input format and then use the format() method to output the date in the desired format.

$date = "31.12.2021";
$dateTime = DateTime::createFromFormat('d.m.Y', $date);
$newDate = $dateTime->format('Y-m-d');
echo $newDate; // Outputs: 2021-12-31