How can the code provided be optimized to handle multiple dates more efficiently?
The code can be optimized by using a loop to iterate over an array of dates instead of repeating the same code for each date individually. This will make the code more efficient and easier to maintain.
$dates = ['2022-01-01', '2022-02-01', '2022-03-01'];
foreach ($dates as $date) {
$dateObj = new DateTime($date);
$formattedDate = $dateObj->format('Y-m-d');
echo $formattedDate . "<br>";
}