-3
9th May 2018    
9th May 2018
9th June 2018
9th August 2018     
8th May 2018
7th June 2018
7th June 2018

How to sort these dates in ascending order and these dates are in a associative array.

Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
gaurav singh
  • 69
  • 1
  • 2
  • 5
  • 1
    Can you provide us with an example of what you've tried so far ? From what you posted, it looks like you didn't try anything yet and just expect us to give you the answer. I would redirect you to the [MySQL doc](https://dev.mysql.com/doc/) then. – linkboss Sep 19 '18 at 10:21
  • by array, it seems that it is a PHP only question – Madhur Bhaiya Sep 19 '18 at 10:22
  • Your question is very unclear. – Fergal Andrews Sep 19 '18 at 10:23
  • 1
    Your edit has made your question clearer. Convert them to timestamps and use array sort. – Fergal Andrews Sep 19 '18 at 10:24
  • Possible duplicate of [How can I sort arrays and data in PHP?](https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php) – Mike Doe Sep 19 '18 at 11:14
  • Possible duplicate of [How to sort date array in php?](https://stackoverflow.com/questions/40462778/how-to-sort-date-array-in-php) – mickmackusa Sep 19 '18 at 12:05

1 Answers1

-1

Using usort you can define any custom sort order, in this case converting the strings into times and comparing them:

$array = array("1" => "9th May 2018",
    "2" => "9th May 2018",
    "3" => "9th June 2018",
    "4" => "9th August 2018",
    "5" => "8th May 2018",
    "6" => "7th June 2018",
    "7" => "7th June 2018",
);

usort($array, function ($a, $b) {
    return strtotime($a) > strtotime($b);
});

var_dump($array);
Barry
  • 3,303
  • 7
  • 23
  • 42
  • downvote with no reason? I've tested that it works... please comment if something is wrong – Barry Sep 19 '18 at 11:21