Sort Array of Dates in YYYY MM DD help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
4dplane
Forum Newbie
Posts: 6
Joined: Wed Nov 25, 2009 11:51 am

Sort Array of Dates in YYYY MM DD help!

Post 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);
jd6th
Forum Newbie
Posts: 7
Joined: Fri Dec 11, 2009 1:50 am

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

Post by jd6th »

are those dates generated from the database, if yes you can sort it in the sql
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
4dplane
Forum Newbie
Posts: 6
Joined: Wed Nov 25, 2009 11:51 am

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

Post 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
Post Reply