What are the best practices for handling date and time formatting in PHP when working with different database systems like MySQL and MSSQL?

When working with different database systems like MySQL and MSSQL in PHP, it's important to handle date and time formatting consistently to ensure data integrity. One way to achieve this is by using the PHP date() function to format dates before inserting them into the database and using the date() function again to format dates retrieved from the database.

// Assuming $date contains the date value to be inserted into the database
$formatted_date = date('Y-m-d H:i:s', strtotime($date));

// Inserting the formatted date into MySQL database
$query = "INSERT INTO table_name (date_column) VALUES ('$formatted_date')";
// Execute the query

// Retrieving the date from MySQL database
$query = "SELECT date_column FROM table_name WHERE condition";
// Execute the query
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$retrieved_date = date('Y-m-d H:i:s', strtotime($row['date_column']));

// $retrieved_date now contains the formatted date retrieved from the database