What are some potential pitfalls to be aware of when sorting entries by date in PHP?

One potential pitfall when sorting entries by date in PHP is that the dates may not be in a consistent format, leading to incorrect sorting results. To avoid this issue, it is important to ensure that all dates are in a standardized format before sorting them.

// Example code snippet to standardize date format before sorting
$dates = ['2021-10-15', '10/20/2021', '2021-09-30', '2021/11/05'];

// Standardize date format to 'Y-m-d'
$standardized_dates = array_map(function($date){
    return date('Y-m-d', strtotime($date));
}, $dates);

// Sort the dates in ascending order
asort($standardized_dates);

// Output sorted dates
foreach($standardized_dates as $date){
    echo $date . "\n";
}