Page 1 of 1

Sort Array of Dates in YYYY MM DD help!

Posted: Wed Dec 16, 2009 1:48 am
by 4dplane
Hi,

I have been trying to use ksort() usort(), mktime() and just good old sort() to sort an array of dates and they are all slapping me in the face saying NO!

Currently, I have parsed a list of dates from a database into YYYY MM DD format and added them all to an array to be sorted with sort but this does not work.

Code: Select all

    $ISOSDates = array();
    //Select the date in the media(api)data and split it into an array
    //Format the date into ISO (YYYY-MM-DD) and use ksort to put it in order
    for($i = 0; $i < sizeof($mediaData); $i++){
        $fullDate = $mediaData[$i]['uploaded_date'];
        //splits the $fulldate array into its components
        list($dayName, $month, $dayNum, $time, $timetype, $year) = split('[ ]', $fullDate);
        //populate the the array with dates to be sorted
        $ISOSDates[$i] = "$year-$month-$dayNum";
    }
    $sortedDates = sort($ISOSDates, SORT_STRING);

Re: Sort Array of Dates in YYYY MM DD help!

Posted: Wed Dec 16, 2009 2:43 am
by jd6th
are those dates generated from the database, if yes you can sort it in the sql

Re: Sort Array of Dates in YYYY MM DD help!

Posted: Wed Dec 16, 2009 2:51 am
by requinix
You can only do a string sort if every year has four digits, every month has two digits, and every day has two digits. Then it has to be arranged with the year appearing before the month appearing before the day.

But yeah: +1 on the "sort it in the SQL" idea.

Re: Sort Array of Dates in YYYY MM DD help!

Posted: Wed Dec 16, 2009 3:38 am
by 4dplane
ok got it! - o and it can't be from a sql database because I don't have access to it/

Code: Select all

    
    $ISOSDates = array();
    $monthnames = array(01 =>"Jan",02 =>"Feb",03 =>"Mar",04 =>"Apr",05 =>"May",06 =>"Jun",07 =>"Jul",08 =>"Aug",09 =>"Sep",10 =>"Oct",11 =>"Nov",12 =>"Dec"); //Select the date in the media(api)data and split it into an array
    //Format the date into ISO (YYYY-MM-DD) and use ksort to put it in order
    for($i = 0; $i < sizeof($mediaData); $i++){
        $fullDate = $mediaData[$i]['uploaded_date'];
        
        //splits the $fulldate array into its components
        list($dayName, $month, $dayNum, $time, $timetype, $year) = split('[ ]', $fullDate);
        for($j = 0; $j < 13; $j++){//13 because the array index is 0 but the the data Jan starts at 1
            if($month === $monthnames[$j]){
                if($j < 10){
                    $monthNum = "0" . $j;
                }else{
                    $monthNum = $j;
                }
            }
        }
        $ISOSDates[$i] = "$year$monthNum$dayNum";
    }
    //sort the dates
    $sortedDates = sort($ISOSDates, SORT_NUMERIC); 
    print_r($ISOSDates);
I hate the dang conditional in there for the month but I could not figure out the php timestamp.

My main issue was the when I posted the question I did not have the data in a YYYYMMDD format!

Thanks,
4dplane