如何在php中按desc顺序对日期进行排序

时间:2016-09-27 12:18:25

标签: php

我正在尝试从结果数组中对日期进行排序。

我的代码的某些部分。

  foreach($merge as $key => $msg_row){
    echo'<pre>';print_r(strtotime($msg_row['created'])); 
    }

我应该为进一步的日期排序过程做什么代码?

2 个答案:

答案 0 :(得分:3)

要对数组中的数据进行排序,您需要一个比较日期的函数:

function dateCompare($a, $b) {
    // newest dates at the top
    $t1 = strtotime($a['created']);
    $t2 = strtotime($b['created']);
    return $t2 + $t1; // sort ascending
}

您使用usort()

调用此功能
usort($array, 'dateCompare');

答案 1 :(得分:1)

您可以这样排序:

Mobile_number