-2

I have an array that looks like this:

( [0] => 03-11-2013 [1] => 04-09-2016 )

How do I order it so that the greatest date always comes first?

Thanks

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
bobo2000
  • 1,779
  • 7
  • 31
  • 54
  • Did you tried anything or do you expect us just to write your code?! – Rizier123 Jan 27 '15 at 16:32
  • Use [usort()](http://php.net/manual/en/function.usort.php) with a comparison callback that converts formatted dates to unix timestamps so that they can be compared easily – Mark Baker Jan 27 '15 at 16:33
  • gazillion posts about this: http://stackoverflow.com/questions/16733128/sort-array-by-date-in-descending-order-by-date-in-php – martskins Jan 27 '15 at 16:42

1 Answers1

1

Here's one:

array_multisort(array_map('strtotime', $array), SORT_DESC, $array);

Convert to timestamps and sort descending, sorting the original array. Might be better to have them as timestamps or YYYY-MM-DD if you control the array creation.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87