What are some best practices for handling date formatting in PHP when retrieving data from a database?

When retrieving date data from a database in PHP, it's important to handle date formatting properly to ensure consistency and accuracy. One best practice is to use PHP's date() function to format the date according to your desired output. Additionally, you can use the strtotime() function to convert the retrieved date string into a Unix timestamp before formatting it.

// Retrieve date from database
$dateFromDB = "2022-01-15 08:30:00";

// Convert date string to Unix timestamp
$timestamp = strtotime($dateFromDB);

// Format the date using date() function
$formattedDate = date("Y-m-d H:i:s", $timestamp);

echo $formattedDate;