2

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');
14
  • 1
    Just do strtotime($a["date"]) - strtotime($b["date"]). You will also need to loop over the elements of the base array. Commented Apr 5, 2019 at 3:14
  • 2
    Missed that, try strtotime($a['startdate']->date) - strtotime($b['startdate']->date) @AmyMcGarity-GermanPearls Commented Apr 5, 2019 at 3:21
  • 1
    $dateObj->getTimestamp() Commented Apr 5, 2019 at 3:22
  • 1
    @Beginner I can't see getTimestamp in the dump, it might not work. Commented Apr 5, 2019 at 3:23
  • 2
    @Phil I can see a field named date in startdate. Commented Apr 5, 2019 at 3:29

1 Answer 1

2

You actually do have errors, you just can't see them (see How to get useful error messages in PHP?)

Your code would be producing

PHP Notice: A non well formed numeric value encountered in ...

This is because you're attempting to subtract one non-numeric string from another.

If you're using PHP 7, you can directly compare DateTime instances with the awesomely named "spaceship operator", eg

return $a['startdate'] <=> $b['startdate'];

enter image description here


If you're stuck on PHP 5, compare timestamps

return $a['startdate']->getTimestamp() - $b['startdate']->getTimestamp();
Sign up to request clarification or add additional context in comments.

4 Comments

No, I'm on php 5.6
@AmyMcGarity-GermanPearls that's why I provided a PHP 5 version
Thank you. I didn't see it - nothing past the image loaded.
@AmyMcGarity-GermanPearls can you see it now? If not, try reloading this page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.