php 如何在php中对日期数组进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40462778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:10:44  来源:igfitidea点击:

How to sort date array in php?

phparrayssortingdate

提问by Mojilo

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 asortbut not working.

我使用asort但不工作。

回答by d.coder

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

演示中检查结果

回答by donald123

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

排序

回答by Blinkydamo

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.

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

回答by Soni Vimalkumar

Try below code:

试试下面的代码:

<?php 

$array = array('11-01-2012','01-01-2011','09-02-2013','01-01-2014','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>";
}
?>

回答by NAVNEET CHANDAN

You definitely can sort array to timestamps with the given code below, but I would like to drag your attention to the format of time I have used to store.. There are different benefits of storing time in such format. Read more about this here https://stackoverflow.com/a/59912185/7997043

您绝对可以使用下面给定的代码将数组排序为时间戳,但我想将您的注意力拖到我用来存储的时间格式上。以这种格式存储时间有不同的好处。在此处阅读更多相关信息https://stackoverflow.com/a/59912185/7997043

function compareByTimeStamp($a, $b ) {
    return strtotime($b) - strtotime($a);
}
$arr = array("2020-02-11 00:00:00", "2020-02-13 00:00:00", "2020-02-08 00:00:00"); 
usort($arr, "compareByTimeStamp"); 
echo json_encode($arr);

回答by MaximeK

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"
  }
}