在PHP中按日期排序数组

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (947)

我试着array日期排序,但结果似乎没有了。如下图所示,0 index应该是最后一张。

结果:

我的代码

$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 PM或者20:13

扫码关注云+社区

领取腾讯云代金券