What are some best practices for handling date and time data from a MySQL database in PHP?

When handling date and time data from a MySQL database in PHP, it is important to ensure that the data is properly formatted and displayed to the user. One common practice is to use the PHP date() function to format the date and time according to the desired output. Additionally, it is recommended to use prepared statements when querying the database to prevent SQL injection attacks.

// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Query to retrieve date and time data
$query = "SELECT date_time FROM table_name";
$result = $mysqli->query($query);

// Fetch and display date and time data
while($row = $result->fetch_assoc()) {
    $date_time = strtotime($row['date_time']);
    echo date('Y-m-d H:i:s', $date_time) . "<br>";
}

// Close database connection
$mysqli->close();