What are the potential pitfalls of using the explode() function to format dates in PHP?

Using the explode() function to format dates in PHP can be problematic because it assumes a specific date format and may not handle all possible date formats. To avoid potential pitfalls, it is recommended to use the DateTime class in PHP, which provides more flexibility and error handling when working with dates.

$dateString = "2022-01-01";
$dateParts = explode("-", $dateString);
$date = new DateTime();
$date->setDate($dateParts[0], $dateParts[1], $dateParts[2]);
$formattedDate = $date->format('Y-m-d');
echo $formattedDate;