0

I have an array like

Array ( [0] => Luke Karizo => Jul 18 2021 [1] => telse Elarazo => Aug 15 2021 [2] => Sarath S => Jul 08 2021 )

I am trying to reorder or sort the elements based on the date values in it. What I have tried is

function date_sort($a, $b) {
    return strtotime($a) - strtotime($b);
}
usort($main_Array, "date_sort");
print_r($main_Array);

I am expecting a result like

Array ( [0] => Sarath S => Jul 08 2021 [1] => Luke Karizo => Jul 18 2021 [2] => telse Elarazo => Aug 15 2021)

Is there any way I can sort this array based on the date values? Any help would be appreciated.

1 Answer 1

1

i am not sure about the array that you are giving, if you could rewrite it properly that would be nice,

but if it is a multidimensional array like i think it is, it should be like :

Array
(
[0] => Array
    (
        [name]     => Luke Karizo,
        [datetime] => Jul 18 2021
    )

[1] => Array
    (
        [name]     => Sarath S,
        [datetime] => Jul 08 2021
    )
...

and this should do the work :

function date_compare($a, $b)
{
    return strtotime($a['datetime']) - strtotime($b['datetime']);
}    
usort($array, 'date_compare');
print_r($array);
Sign up to request clarification or add additional context in comments.

2 Comments

Tried this and not getting the result as expected. Please see below Array ( [0] => Array ( [name] => Luke Karizo [birth_Day] => 1942/07/18 ) [1] => Array ( [name] => Natelse Elarazo [birth_Day] => 1947/08/15 ) [2] => Array ( [name] => Sarath S [birth_Day] => 1990/07/08 ) )
Sorry, it's working I forgot to change the year to the current year.

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.