Sorting array by date in PHP

37
August 19, 2018, at 11:30 AM

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:

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);
Answer 1

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.

READ ALSO
PHP - why should I use pathinfo when I can get it through $_File array

PHP - why should I use pathinfo when I can get it through $_File array

why should I use this code to get the name of the file?

32
How to enhance C.R.A.P. index for a switch-like function?

How to enhance C.R.A.P. index for a switch-like function?

I have a very typical switch-like function that returns a clasiffication for a given input value (Body Mass Index in this case)(I'm working with this function, but it could be any other of the same nature)

46
FPM pool.d does not use default php.ini values

FPM pool.d does not use default php.ini values

environment: ubuntu, nginx, php72

55