sorting of associate array by date

Posted: July 17, 2014 in PHP, PHP Interview Questions
// sample $arrDate array

Array ( [0] => stdClass Object ( [date] => 2013-06-22 ) [1] => stdClass Object ( [date] => 2013-06-29 )
)



Use the following code to sort the associate array by date as one field

function sortByDate($a,$b){
                $t1 = strtotime($a->date);
                $t2 = strtotime($b->date);
                if ($t1 == $t2) {
                return 0;
                }
                return ($t1 < $t2) ? -1 : 1;
        }
        
    usort($arrData,"sortByDate");

Leave a comment