How can the issue of sorting dates be resolved in PHP when retrieving data from a MySQL database?

When retrieving dates from a MySQL database in PHP, the dates may not be sorted correctly due to the default format in which dates are stored in the database. To resolve this issue, you can use the `STR_TO_DATE()` function in your MySQL query to convert the date string into a format that can be properly sorted. This function allows you to specify the format of the date string, ensuring that the dates are sorted correctly when retrieved in PHP.

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

if(mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        // Process retrieved data
    }
}