phpsortingstrtotimeasort

Sorting an Array of Dates using asort()


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


Solution

  • For some reason european - didn't worked for 98 so i recommend edit year to 4 digit number format:

        function sortArrays_ByDate($target){
    
            foreach($target as $key_s => $val_s) {
                $day = substr($val_s, 0, strpos($val_s, '.'));
                $month = substr($val_s, strpos($val_s, '.')+1, strrpos($val_s, '.')-strpos($val_s, '.')-1);
                $year = substr($val_s,  strrpos($val_s, '.')+1);
    
                if($year > 79 && $year <= 99)
                    $year = "19" . $year;
                elseif($year >=00 && $year <= 79)
                    $year = "20" . $year;
    
                $target[(string)$key_s] = strtotime($day .'.'. $month .'.'. $year);
            }
    
            asort($target);
    
            /*foreach($target as $value)
                echo date("d.m.Y", $value) . '</br>';
            changed for edit time to d.m.Y ->
            */
    
            foreach($target as $key_s => $val_s)
            $target[(string)$key_s] = date("d.m.Y", $target[(string)$key_s]);
    
            return $target;
    }
    

    Then it worked for me, and array is sorted correctly.

    Edit //

    result is:

    Array
    (
        [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
    )
    Array
    (
        [6] => 12.09.1998
        [3.1] => 23.04.2010
        [4] => 28.09.2012
        [2.0] => 28.09.2012
        [2.1] => 29.09.2012
        [2.2] => 29.09.2012
        [1] => 02.10.2012
        [3] => 09.10.2012
        [0] => 09.10.2012
        [5] => 26.10.2012
        [6.0] => 05.03.2013
        [6.1] => 23.04.2013
        [2] => 27.09.2015
    )