What are some best practices for storing date and time values in a database, and why is it important to use appropriate data types for such values?

When storing date and time values in a database, it is important to use appropriate data types to ensure data integrity and accurate sorting and filtering. One best practice is to use the DATETIME data type for storing both date and time values together. This ensures that the values are stored in a standardized format and can be easily manipulated and queried.

// Create a table with a DATETIME column to store date and time values
$createTableQuery = "CREATE TABLE events (
                        id INT AUTO_INCREMENT PRIMARY KEY,
                        event_name VARCHAR(255),
                        event_datetime DATETIME
                    )";
mysqli_query($conn, $createTableQuery);

// Insert a new event with the current date and time
$insertEventQuery = "INSERT INTO events (event_name, event_datetime) 
                    VALUES ('New Event', NOW())";
mysqli_query($conn, $insertEventQuery);