在PHP中按日期排序数组

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在PHP中按日期排序数组相关的知识,希望对你有一定的参考价值。

我试着按日期对array进行排序,但结果似乎没有了。正如你在下面的图片中看到的那样,0 index应该在最后。

结果:

enter image description here

我的守则

$next_Data = json_decode('[{"EmployeeID":102,"DateTimeRecord":"7/31/2018 9:39:13 PM","InOut":"0"},
                        {"EmployeeID":102,"DateTimeRecord":"7/31/2018 9:39:13 PM","InOut":"0"},
                        {"EmployeeID":102,"DateTimeRecord":"7/31/2018 9:39:13 PM","InOut":"0"},
                        {"EmployeeID":102,"DateTimeRecord":"8/1/2018 6:43:04 AM","InOut":"1"},
                        {"EmployeeID":102,"DateTimeRecord":"8/1/2018 6:43:05 AM","InOut":"1"},
                        {"EmployeeID":102,"DateTimeRecord":"8/1/2018 6:43:06 AM","InOut":"1"},
                        {"EmployeeID":102,"DateTimeRecord":"8/1/2018 6:13:25 PM","InOut":"0"},
                        {"EmployeeID":102,"DateTimeRecord":"8/1/2018 6:13:25 PM","InOut":"0"},
                        {"EmployeeID":102,"DateTimeRecord":"8/2/2018 20:13:25 PM","InOut":"1"},
                        {"EmployeeID":102,"DateTimeRecord":"8/1/2018 6:13:25 PM","InOut":"0"},
                        {"EmployeeID":102,"DateTimeRecord":"8/2/2018 6:54:12 AM","InOut":"1"},
                        {"EmployeeID":102,"DateTimeRecord":"8/2/2018 6:54:12 AM","InOut":"1"},
                        {"EmployeeID":102,"DateTimeRecord":"8/2/2018 6:54:12 AM","InOut":"1"},
                        {"EmployeeID":102,"DateTimeRecord":"8/2/2018 12:13:25 PM","InOut":"0"}]');


$next_Data = array_map("unserialize", array_unique(array_map("serialize", $next_Data)));

        foreach ($next_Data as $key => $part) 
        {
            $sort[$key] = strtotime($part->DateTimeRecord);
        }

array_multisort($sort, SORT_ASC, $next_Data);
答案

strtotime()为此值返回FALSE,因此它被排序为0,因此首先出现。 20:13 PM不是有效时间;它应该是8:13 PM20:13

以上是关于在PHP中按日期排序数组的主要内容,如果未能解决你的问题,请参考以下文章

在PHP中按数组键的值对字典中的数组进行排序

如何在php中按降序对数组进行排序?

PHP - 从数据库构建多级关联数组(从数据库中按州对城市进行排序)

如何在PHP中按值对对象数组进行排序

创建 PHP 数组,其中包含从 SQL 中按月提取的日期和订单数

在 PHP 中按整数键对对象进行排序