datetime - PHP Sort a multidimensional array by element containing date -
i have array such as:
array ( [0] => array ( [id] => 2 [type] => comment [text] => hey [datetime] => 2010-05-15 11:29:45 ) [1] => array ( [id] => 3 [type] => status [text] => oi [datetime] => 2010-05-26 15:59:53 ) [2] => array ( [id] => 4 [type] => status [text] => yeww [datetime] => 2010-05-26 16:04:24 ) )
can suggest way sort/order based on datetime element?
use usort()
, custom comparing function:
function date_compare($a, $b) { $t1 = strtotime($a['datetime']); $t2 = strtotime($b['datetime']); homecoming $t1 - $t2; } usort($array, 'date_compare');
edit: info organized in array of arrays. improve distinguish those, let's phone call inner arrays (data) records, info array of records.
usort
pass 2 of these records given comparing function date_compare()
@ a time. date_compare
extracts "datetime"
field of each record unix timestamp (an integer), , returns difference, result 0
if both dates equal, positive number if first 1 ($a
) larger or negative value if sec argument ($b
) larger. usort()
uses info sort array.
php datetime arrays sorting
No comments:
Post a Comment