Sort Time & Date inside a multidimensional array()

uksort() will sort the keys of an array using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function. .


function mysort($a, $b)
{
   return(strtotime($b['timestamp']) - strtotime($a['timestamp']));
}


// Convert first date & time to UNIX timestamp

$t1 = mktime(2, 30, 0, 7, 1, 2011);
$t2 = mktime(3, 30, 0, 7, 1, 2011);
$t3 = mktime(4, 30, 0, 7, 1, 2011);


// Store data to a Multidimensional Arrays
$testArray = array(
    "testing3" => array("timestamp" => $t1, 
                        "name" =>"testing3"),
                         
    "testing" => array("timestamp" => $t2, 
                       "name" =>"testing"),
                        
    "testing2" => array("timestamp" => $t3, 
                        "name" =>"testing2")
    );
                          


usort($testArray, 'mysort');

echo "<pre>";
print_r($testArray);

echo "<br /><br />";

foreach($testArray AS $row){
 
 $date = $row['timestamp'];
 echo date("d/m/y : H:i:s a",$date)."<br />";
}

OUTPUT
Array
(
    [0] => Array
        (
            [timestamp] => 1309519800
            [name] => testing2
        )

    [1] => Array
        (
            [timestamp] => 1309516200
            [name] => testing
        )

    [2] => Array
        (
            [timestamp] => 1309512600
            [name] => testing3
        )

)


01/07/11 : 04:30:00 am
01/07/11 : 03:30:00 am
01/07/11 : 02:30:00 am

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

0 comments: on "Sort Time & Date inside a multidimensional array()"

:) ) ;) ) ;;) :D ;) :p :( ( :) :( :X =(( :-o :-/ :-* :| 8-} :) ] ~x( :-t b-( :-L x( =))
Post a Comment