What is the best way to format date and time retrieved from a MSSQL database in PHP?

When retrieving date and time data from a MSSQL database in PHP, it is recommended to use the DateTime class to format the date and time according to your desired output. This allows for flexibility in displaying the date and time in various formats. By using the format() method of the DateTime class, you can easily customize the date and time display.

// Retrieve date and time from MSSQL database
$rawDateTime = $row['date_time'];

// Create a DateTime object
$dateTime = new DateTime($rawDateTime);

// Format the date and time as needed
$formattedDateTime = $dateTime->format('Y-m-d H:i:s');

// Output the formatted date and time
echo $formattedDateTime;