phparrayssortingdatetimemultidimensional-array

Sort a 2d array by datetime column formatted as d/m/Y H:i:s A


I'm tryin' to sort an array by date, but the result seems off. As you can see in the picture below the 0 index should be in the last.

Result:

enter image description here

My Code

$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);

Solution

  • strtotime() returns FALSE for this value, so it is sorted as a 0, and thus appears first. 20:13 PM is not a valid time; it should be either 8:13 PM or 20:13.