I have this array with dates. All the dates are checked before creating the array with preg_match and check_dates.
GoodDates (
[0] => 2021-09-01
[1] => 2021-09-02
[2] => 2021-09-03
[3] => 2021-09-06
[4] => 2021-09-07
[5] => 2021-09-08
[6] => 2021-09-09
[7] => 2021-09-10
[8] => 2021-08-11
[9] => 2021-09-11
[10] => 2021-09-12
[11] => 2021-08-13
[12] => 2021-08-16
[13] => 2021-08-17
[14] => 2021-08-18
[15] => 2021-08-19
[16] => 2021-08-20
[17] => 2021-08-21
[18] => 2021-08-23
[19] => 2021-08-24
[20] => 2021-08-27
[21] => 2021-08-28
[22] => 2021-08-29
[23] => 2021-08-30
[24] => 2021-08-31 )
First, I wrote this to rsort the array $GoodDates and it works fine.
foreach ($GoodDates as $dateitem){
$strtotimeGoodDates[] = strtotime($dateitem);
}
rsort($strtotimeGoodDates);
foreach ($strtotimeGoodDates as $DatesForstrftime){
$GoodDatesSorted[] = strftime("%Y-%d-%B",$DatesForstrftime);
}
print_r($GoodDatesSorted);
I find this method and it works too.
$compare_function = function($a,$b) {
$a_timestamp = strtotime($a);
$b_timestamp = strtotime($b);
return $b_timestamp <=> $a_timestamp;
};
usort($GoodDates, $compare_function);
print_r($GoodDates);
Is my solution a good practice or it is better to use usort and why ?
checkdateloop. - I have a better comprehension of how to sort a date array and some differents methods to do that. Thanks to everybody. \$\endgroup\$