如何在PHP中排序日期数组?

时间:2021-05-22 15:56:11

I am new to php, I have php date array

我是php新手,我有php日期数组

[0] => 11-01-2012
[1] => 01-01-2014
[2] => 01-01-2015
[3] => 09-02-2013
[4] => 01-01-2013

I want to sort it like :

我想把它排序为:

[0] => 11-01-2012
[1] => 01-01-2013
[2] => 09-02-2013
[3] => 01-01-2014
[4] => 01-01-2015

I use asort but not working.

我使用asort但不工作。

5 个解决方案

#1


12  

Because array items is string, you need to convert them to date and then comparing to sort. The usort() sort array using custom function that is a good sort function for this case.

因为数组项是字符串,所以需要将它们转换为日期,然后比较排序。 usort()排序数组使用自定义函数,对于这种情况是一个很好的排序函数。

$arr = array('11-01-2012', '01-01-2014', '01-01-2015', '09-02-2013', '01-01-2013');    
function date_sort($a, $b) {
    return strtotime($a) - strtotime($b);
}
usort($arr, "date_sort");
print_r($arr);

Check result in demo

在演示中检查结果

#2


2  

Try below code:

试试以下代码:

<?php 

$array[0] = '11-01-2012';
$array[1] = '01-01-2011';
$array[2] = '09-02-2013';
$array[3] = '01-01-2014';
$array[4] = '01-01-2015';

function cmp($a, $b)
{
    $a = date('Y-m-d', strtotime($a));
    $b = date('Y-m-d', strtotime($b));

    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}
usort($array, "cmp");

foreach ($array as $key => $value) {
    echo "[$key]=> $value <br>";
}
?>

#3


1  

out of the box use time function to generate ts and sort

开箱即用使用时间函数来生成ts和排序

<?php
  $out = array();
  // $your_array is the example obove
  foreach($your_array as $time) {
  $out[strtotime($time)] = $time;
  }
  // now $out has ts-keys and you can handle it

  ...
 ?>

ksort

ksort

#4


0  

use DateTime for sorting date :

使用DateTime排序日期:

$a = array(
    new DateTime('2016-01-02'),
    new DateTime('2016-05-01'),
    new DateTime('2015-01-01'),
    new DateTime('2016-01-01')
);
asort($a);
var_dump($a);

The output would be :

输出将是:

array(4) {
  [2]=>
  object(DateTime)#3 (3) {
    ["date"]=>
    string(26) "2015-01-01 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "US/Pacific"
  }
  [3]=>
  object(DateTime)#4 (3) {
    ["date"]=>
    string(26) "2016-01-01 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "US/Pacific"
  }
  [0]=>
  object(DateTime)#1 (3) {
    ["date"]=>
    string(26) "2016-01-02 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "US/Pacific"
  }
  [1]=>
  object(DateTime)#2 (3) {
    ["date"]=>
    string(26) "2016-05-01 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "US/Pacific"
  }
}

#5


0  

Try this,

尝试这个,

<?php 
$array = [ '11-01-2012', '01-01-2014', '01-01-2015', '09-02-2013', '01-01-2013' ];
function sortFunction( $a, $b ) {
    return strtotime($a) - strtotime($b);
}
usort($array, "sortFunction");
var_dump( $array );
?>

Will sort the dates into the order you want.

将日期排序为您想要的顺序。

#1


12  

Because array items is string, you need to convert them to date and then comparing to sort. The usort() sort array using custom function that is a good sort function for this case.

因为数组项是字符串,所以需要将它们转换为日期,然后比较排序。 usort()排序数组使用自定义函数,对于这种情况是一个很好的排序函数。

$arr = array('11-01-2012', '01-01-2014', '01-01-2015', '09-02-2013', '01-01-2013');    
function date_sort($a, $b) {
    return strtotime($a) - strtotime($b);
}
usort($arr, "date_sort");
print_r($arr);

Check result in demo

在演示中检查结果

#2


2  

Try below code:

试试以下代码:

<?php 

$array[0] = '11-01-2012';
$array[1] = '01-01-2011';
$array[2] = '09-02-2013';
$array[3] = '01-01-2014';
$array[4] = '01-01-2015';

function cmp($a, $b)
{
    $a = date('Y-m-d', strtotime($a));
    $b = date('Y-m-d', strtotime($b));

    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}
usort($array, "cmp");

foreach ($array as $key => $value) {
    echo "[$key]=> $value <br>";
}
?>

#3


1  

out of the box use time function to generate ts and sort

开箱即用使用时间函数来生成ts和排序

<?php
  $out = array();
  // $your_array is the example obove
  foreach($your_array as $time) {
  $out[strtotime($time)] = $time;
  }
  // now $out has ts-keys and you can handle it

  ...
 ?>

ksort

ksort

#4


0  

use DateTime for sorting date :

使用DateTime排序日期:

$a = array(
    new DateTime('2016-01-02'),
    new DateTime('2016-05-01'),
    new DateTime('2015-01-01'),
    new DateTime('2016-01-01')
);
asort($a);
var_dump($a);

The output would be :

输出将是:

array(4) {
  [2]=>
  object(DateTime)#3 (3) {
    ["date"]=>
    string(26) "2015-01-01 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "US/Pacific"
  }
  [3]=>
  object(DateTime)#4 (3) {
    ["date"]=>
    string(26) "2016-01-01 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "US/Pacific"
  }
  [0]=>
  object(DateTime)#1 (3) {
    ["date"]=>
    string(26) "2016-01-02 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "US/Pacific"
  }
  [1]=>
  object(DateTime)#2 (3) {
    ["date"]=>
    string(26) "2016-05-01 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "US/Pacific"
  }
}

#5


0  

Try this,

尝试这个,

<?php 
$array = [ '11-01-2012', '01-01-2014', '01-01-2015', '09-02-2013', '01-01-2013' ];
function sortFunction( $a, $b ) {
    return strtotime($a) - strtotime($b);
}
usort($array, "sortFunction");
var_dump( $array );
?>

Will sort the dates into the order you want.

将日期排序为您想要的顺序。