4

I have an array like thus i want to sort this array date-wise how to sort the this array

Array
(
[0] => 28/02/2016
[1] => 30/01/2016
[2] => 16/02/2016
[3] => 19/02/2016
[4] => 24/02/2016
[5] => 13/02/2016
[6] => 18/02/2016
[7] => 27/02/2016
[8] => 25/02/2016
[9] => 01/02/2016
[10] => 02/02/2016
[11] => 03/02/2016
[12] => 05/02/2016
[13] => 06/02/2016
[14] => 07/02/2016
[15] => 08/02/2016
[16] => 11/02/2016
[17] => 12/02/2016
)

I have used usort but it does not work in which i am wirting a function and and converting it to strtotime but it does not work .Any suggestion.

Fabio
  • 23,183
  • 12
  • 55
  • 64
RAJ
  • 229
  • 2
  • 14
  • 1
    Show the function you have written. And replacing `/` with `-` might solve the issue. – Sougata Bose Mar 23 '16 at 07:22
  • 1
    Check this it may useful. http://stackoverflow.com/questions/16733128/sort-array-by-date-in-descending-order-by-date-in-php @user3270582 – RJParikh Mar 23 '16 at 07:22
  • 1
    change the format to `d-m-Y` so that it can be accormodated by `strottime` like sougata said and it'll work – Kevin Mar 23 '16 at 07:24
  • Possible duplicate of [PHP Sort a multidimensional array by element containing date](http://stackoverflow.com/questions/2910611/php-sort-a-multidimensional-array-by-element-containing-date) – Rahul Singh Mar 23 '16 at 07:26
  • Yes str_replace / with - is workings thanks – RAJ Mar 23 '16 at 07:27
  • Do you really need to do a strtotime? If you use Ymd then all later dates have a higher number? – Andreas Mar 23 '16 at 07:30

2 Answers2

3

Try below solution:

$array = array
(
    0 => '28/02/2016',
    1 => '30/01/2016',
    2 => '16/02/2016',
    3 => '19/02/2016',
    4 => '24/02/2016',
    5 => '13/02/2016',
    6 => '18/02/2016',
    7 => '27/02/2016',
    8 => '25/02/2016',
    9 => '01/02/2016',
    10 => '02/02/2016',
    11 => '03/02/2016',
    12 => '05/02/2016',
    13 => '06/02/2016',
    14 => '07/02/2016',
    15 => '08/02/2016',
    16 => '11/02/2016',
    17 => '12/02/2016'
);

function sortFunction( $a, $b ) {
    $date1 = DateTime::createFromFormat('d/m/Y', $a);

    $date2 = DateTime::createFromFormat('d/m/Y', $b);
    return $date1->getTimestamp() - $date2->getTimestamp();
}
usort($array, "sortFunction");
print_r($array);

output

Array
(
    [0] => 30/01/2016
    [1] => 01/02/2016
    [2] => 02/02/2016
    [3] => 03/02/2016
    [4] => 05/02/2016
    [5] => 06/02/2016
    [6] => 07/02/2016
    [7] => 08/02/2016
    [8] => 11/02/2016
    [9] => 12/02/2016
    [10] => 13/02/2016
    [11] => 16/02/2016
    [12] => 18/02/2016
    [13] => 19/02/2016
    [14] => 24/02/2016
    [15] => 25/02/2016
    [16] => 27/02/2016
    [17] => 28/02/2016
)
Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44
1

Try this using sort()

$array = array
(
    0 => '28/02/2016',
    1 => '30/01/2016',
    2 => '16/02/2016',
    3 => '19/02/2016',
    4 => '24/02/2016',
    5 => '13/02/2016',
    6 => '18/02/2016',
    7 => '27/02/2016',
    8 => '25/02/2016',
    9 => '01/02/2016',
    10 => '02/02/2016',
    11 => '03/02/2016',
    12 => '05/02/2016',
    13 => '06/02/2016',
    14 => '07/02/2016',
    15 => '08/02/2016',
    16 => '11/02/2016',
    17 => '12/02/2016'
);

foreach($array as $key=>$val) {
    $date_arr=explode('/',$val);
    $time_arr[$key]=strtotime($date_arr[2].'/'.$date_arr[1].'/'.$date_arr[0]);
}
sort($time_arr);
foreach($time_arr as $key=>$val) {
    $array[$key]=date("d/m/Y", $val);
}
echo '<pre>'; print_r($array); echo '</pre>';

Output

Array
(
    [0] => 30/01/2016
    [1] => 01/02/2016
    [2] => 02/02/2016
    [3] => 03/02/2016
    [4] => 05/02/2016
    [5] => 06/02/2016
    [6] => 07/02/2016
    [7] => 08/02/2016
    [8] => 11/02/2016
    [9] => 12/02/2016
    [10] => 13/02/2016
    [11] => 16/02/2016
    [12] => 18/02/2016
    [13] => 19/02/2016
    [14] => 24/02/2016
    [15] => 25/02/2016
    [16] => 27/02/2016
    [17] => 28/02/2016
)
Thiyagesan
  • 75
  • 6