How does MySQL interpret the result of DATE_FORMAT when sorting dates?

When sorting dates in MySQL that have been formatted using DATE_FORMAT, MySQL will interpret the dates as strings rather than actual dates. This can lead to unexpected sorting results, as strings are sorted lexicographically rather than chronologically. To solve this issue, you can use the STR_TO_DATE function in your ORDER BY clause to convert the formatted dates back to actual dates for proper sorting.

$query = "SELECT * FROM table_name ORDER BY STR_TO_DATE(date_column, '%Y-%m-%d')";
$result = mysqli_query($connection, $query);

if($result) {
    while($row = mysqli_fetch_assoc($result)) {
        // Process each row
    }
} else {
    echo "Error: " . mysqli_error($connection);
}