且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何排序在PHP中的日期数组

更新时间:2022-06-02 22:43:07

使用的ISO( YYYY-MM-DD )格式,而不是英语的格式,然后只需使用 ksort 功能让他们在正确的顺序。

Use the ISO (yyyy-mm-dd) format rather than the "english" format, and then just use the ksort function to get them in the right order.

有没有需要删除的连字符, ksort 会做的字符串键字母数字的比较,以及 YYYY-MM-DD 格式完美的作品以及词法顺序是一样的实际日期顺序。

There's no need to remove the hyphens, ksort will do an alphanumeric comparison on the string keys, and the yyyy-mm-dd format works perfectly well as the lexical order is the same as the actual date order.

修改我现在看到你们已经纠正你的问题,表明你实际上得到一个数组的数组,并且sort关键是在子阵列。在这种情况下,你应该使用 uksort 其他地方推荐的,但我会建议你去用自己的编辑和排序基于DB格式化的日期,而不是​​通过解析人类可读的格式为:

EDIT I see you've now corrected your question to show that you've actually got an array of arrays, and that the sort key is in the sub-arrays. In this case, you should use uksort as recommended elsewhere, but I would recommend that you go with your own edit and sort based on the DB formatted date, rather than by parsing the human readable format:

function cmp($a, $b)
{
    global $array;
    return strcmp($array[$a]['db'], $array[$b]['db']);
}

uksort($array, 'cmp');