I have an array. Examples of the string values inside the array are
10-01-2019
28-12-2018
16-01-2019
21-01-2019
14-11-2018
I need to list these in date order. I couldn't sort them based on how they were named because 28-12-2018 would be seen as greater than 10-01-2019 for example (as 28 is larger than 10) when in fact it's not, as the first value is for December the previous year.
Therefore I made tweaks to display the values as as YYYYMMDD
20190110
20181228
20190116
20190121
20181114
However I cannot get the values to order properly when looping through them
//this is my array
$row['file_name'];
//remove hyphens from file name and display only 8 characters
$date = substr(str_replace("-", "", $row['sitma_file_name']), 0, 8);
//get the year part of $date
$year = substr($date,4);
//get the month part of $date
$month = substr($date,2, 2);
//get the day part of $date
$day = substr($date, 0, 2);
//concatenate above variables to make $date display as YYYYMMDD
$date = $year . $month . $day;
//put $date in an array
$date_array = array($date);
//sort the array
sort($date_array);
//loop through array and echo values
foreach ($date_array as $value){
echo $value;
}
Expected results are
20181114
20181228
20190110
20190116
20190121
However the actual results are
20190110
20181114
20190116
20190121
20181228