I have a long multi dimensional array including some DateTime objects. I want to sort the entire array based on the [startdate] object. I'm using this usort function (based on a lot of online resources and modifications from various errors). It's not sorting the array.
My array is formatted as follows. Showing only the start for brevity.
Array
(
[] => Array
(
[startdate] => DateTime Object
(
[date] => 2019-04-10 19:00:00.000000
[timezone_type] => 3
[timezone] => America/New_York
)
[starttime] => DateTime Object
(
[date] => 2019-04-10 19:00:00.000000
[timezone_type] => 3
[timezone] => America/New_York
)
Here is my usort code:
function date_compare($a, $b) {
$t1 = $a["startdate"]->format('Y-m-d H:i:s');
$t2 = $b["startdate"]->format('Y-m-d H:i:s');
return $t1 - $t2;
}
usort($allEventsSimple, 'date_compare');
I also tried sorting the DateTime objects using this code:
function date_compare($a, $b) {
$t1 = var_dump($a[startdate]);
$t2 = var_dump($b[startdate]);
return $t1 - $t2;
}
usort($allEventsSimple, 'date_compare');