Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to sort date using usort

Tags:

php

php4

I have user defined function sort which sorts by date. It works fine for the same year and does not work when the year changes. This an example of the dates that is sorted by the function:

 01/02/2013  
 01/03/2013 
 12/12/2012  

 function mysort($a,$b) {
   return $a['date']>$b['date'];
 }

 usort($arr,"mysort");

This is an old application which uses php4. Is there a way where I can compare dates directly?

like image 653
Micheal Avatar asked Jun 11 '26 00:06

Micheal


1 Answers

Use strtotime.

function mysort($a,$b) {
    return strtotime($a['date'])>strtotime($b['date']);
}
like image 200
yajakass Avatar answered Jun 17 '26 11:06

yajakass



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!