2016-03-23 11 views
4

ich ein Array haben wie so ich dieses Array Datum weise sortiert werden soll, wie die dieses ArraySortierung ein Array enthalten Daten in PHP

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 usort verwendet haben zu sortieren, aber es funktioniert nicht in dem i bin eine Funktion und und konvertieren es in strtotime, aber es funktioniert nicht. Ein Vorschlag.

+1

Zeigen Sie die Funktion, die Sie geschrieben haben. Und das Ersetzen von '/' mit '-' könnte das Problem lösen. –

+1

Überprüfen Sie dies möglicherweise nützlich. http://stackoverflow.com/questions/16733128/sort-array-by-date-in-descending-order-by-date-in-php @ user3270582 – RJParikh

+1

ändern Sie das Format zu "dmY", so dass es von 'strottime' wie sougata sagte und es wird funktionieren – Ghost

Antwort

3

unten versuchen Lösung:

$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); 

Ausgang

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 
) 
1

Versuchen dies mit 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 
)