What are the potential pitfalls of storing dates and times in separate columns as varchar in a database?

Storing dates and times in separate columns as varchar in a database can lead to inconsistencies, difficulty in querying and sorting data, and increased storage space. To solve this issue, it is recommended to store dates and times in a single datetime or timestamp column in the database.

// Create a table with a single datetime column to store dates and times
$query = "CREATE TABLE events (
    event_datetime DATETIME
)";
mysqli_query($connection, $query);

// Insert a date and time into the table
$datetime = "2022-01-01 12:00:00";
$query = "INSERT INTO events (event_datetime) VALUES ('$datetime')";
mysqli_query($connection, $query);

// Retrieve events ordered by datetime
$query = "SELECT * FROM events ORDER BY event_datetime";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
    echo $row['event_datetime'] . "<br>";
}