I'm really confused, I tried a lot of related approaches to sort an array of dates that may have different formats.
I have an array of dates, for example:
"0"=>"09.10.2012"
"1"=>"02.10.12"
"2"=>"27.09.15"
"2.0"=>"28.09.2012"
"2.1"=>"29.9.2012"
"2.2"=>"29.09.2012"
"3"=>"9.10.2012"
"3.1"=>"23.4.10"
"4"=>"28.09.2012"
"5"=>"26.10.2012"
"6"=>"12.09.98"
"6.0"=>"05.03.2013"
"6.1"=>"23.4.2013"
(the keys are strings for a reason)
Now I know that they will be in the same format order - days, month, years . But the digits number can change as you can see in the given array.
I basically parsed them to day-month-year (European format that strtotime()
recognize according to documentation) and then changed them to a Unix time-stamp, I'm sorting the array using asort()
and I received bad results:
[6]->[] -- 12.09.98
[1]->[1034380800] -- 02.10.12
[2.0]->[1348790400] -- 28.09.2012
[4]->[1348790400] -- 28.09.2012
[2.2]->[1348876800] -- 29.09.2012
[2.1]->[1348876800] -- 29.9.2012
[3]->[1349740800] -- 9.10.2012
[0]->[1349740800] -- 09.10.2012
[5]->[1351209600] -- 26.10.2012
[6.0]->[1362441600] -- 05.03.2013
[6.1]->[1366675200] -- 23.4.2013
[3.1]->[1681084800] -- 23.4.10
[2]->[1820966400] -- 27.09.15
As you can see [6](unixtime)
contains False and strtotime()
is failing converting the dates.
here is my code:
function sortArrays_ByDate($target){
foreach($target as $key_s => $val_s) { $date_exp = preg_replace('#(\.|_)#','-',$val_s); $target[(string)$key_s] = $date_exp; }
foreach($target as $key_s => $val_s) { $date_exp = strtotime($val_s); $target[(string)$key_s] = $date_exp; }
asort($target);
return $target;
}
Can some one please explain me what is wrong...
Thanks
For some reason european - didn't worked for 98 so i recommend edit year to 4 digit number format:
Then it worked for me, and array is sorted correctly.
Edit //
result is: