What are some best practices for formatting dates in PHP output, especially when retrieving them from a MySQL database?

When retrieving dates from a MySQL database in PHP, it's important to format them correctly for display. One common best practice is to use the date() function along with strtotime() to convert the MySQL date format into a more human-readable format. Another approach is to use the DateTime class to format dates in a more flexible and object-oriented way.

// Retrieve date from MySQL database
$mysqlDate = "2022-01-15";

// Using date() function
$formattedDate = date("F j, Y", strtotime($mysqlDate));
echo $formattedDate;

// Using DateTime class
$date = new DateTime($mysqlDate);
$formattedDate = $date->format('F j, Y');
echo $formattedDate;