php - issue in sorting a date array -
this question has answer here:
- how can sort arrays , data in php? 7 answers
i want sort array on date basis.
i used below code sort array
<?php $a=array("14-10-2013","15-10-2013","16-10-2013","13-11-2013","17-11-2013","18-10-2013","19-10-2013"); array_multisort($a); print_r($a); ?>
this gives me result
array ( [0] => 13-11-2013 [1] => 14-10-2013 [2] => 15-10-2013 [3] => 16-10-2013 [4] => 17-11-2013 [5] => 18-10-2013 [6] => 19-10-2013 )
this not correct order.
the correct order should be
array ( [0] => 14-10-2013 [1] => 15-10-2013 [2] => 16-10-2013 [3] => 18-10-2013 [4] => 19-10-2013 [5] => 13-11-2013 [6] => 17-11-2013 )
what should have correct sequence of date?
use code :
$a=array("14-10-2013","15-10-2013","16-10-2013","13-11-2013","17-11-2013","18-10-2013","19-10-2013"); usort($a, "sortfunction"); print_r($a); function sortfunction( $a, $b ) { return strtotime($a) - strtotime($b); } output array ( [0] => 14-10-2013 [1] => 15-10-2013 [2] => 16-10-2013 [3] => 18-10-2013 [4] => 19-10-2013 [5] => 13-11-2013 [6] => 17-11-2013 )
Comments
Post a Comment