What are some common pitfalls when sorting dates in PHP without using "time()"?

When sorting dates in PHP without using "time()", a common pitfall is that dates are compared as strings rather than as actual date values. This can lead to incorrect sorting results, especially when dates are in different formats or timezones. To solve this issue, you can convert the dates to Unix timestamps using the strtotime() function before sorting them.

$dates = ['2022-01-15', '2022-01-10', '2022-01-20'];

usort($dates, function($a, $b) {
    return strtotime($a) - strtotime($b);
});

print_r($dates);