在PHP中按日期排序数组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在PHP中按日期排序数组相关的知识,希望对你有一定的参考价值。
我试着按日期对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
。
以上是关于在PHP中按日期排序数组的主要内容,如果未能解决你的问题,请参考以下文章
PHP - 从数据库构建多级关联数组(从数据库中按州对城市进行排序)