【问题标题】:How to sort date array in php?如何在php中对日期数组进行排序?
【发布时间】:2017-03-20 15:42:56
【问题描述】:

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

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

我想按如下方式排序:

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

我使用asort 但不工作。

【问题讨论】:

标签: php arrays sorting date


【解决方案1】:

因为数组项是字符串,所以需要将它们转换为日期,然后比较排序。使用自定义函数的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);

检查结果在demo

【讨论】:

    【解决方案2】:

    开箱即用使用时间函数生成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

    【讨论】:

      【解决方案3】:

      试试这个,

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

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

      【讨论】:

      • 试试这个(仅代码)答案在 Stackoverflow 上的价值很低,因为它们对教育/授权 OP 和未来的研究人员几乎没有什么作用。请说明您的解决方案的工作原理以及为什么它是一个好主意。
      【解决方案4】:

      试试下面的代码:

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

      【讨论】:

      • 试试这个(仅代码)答案在 Stackoverflow 上的价值很低,因为它们对教育/授权 OP 和未来的研究人员几乎没有作用。请说明您的解决方案的工作原理以及为什么它是一个好主意。
      【解决方案5】:

      您绝对可以使用下面给定的代码将数组排序为时间戳,但我想提醒您注意我用来存储时间的格式。以这种格式存储时间有不同的好处。在此处阅读更多信息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);
      

      【讨论】:

        【解决方案6】:

        我通过应用这条线解决了这个问题。

        sort($imdarrayGG['newDate']);
        

        加值前的输出

        [Mon Jul  6 10:33:23 2020] Array
        (
            [coursedate] => 2020-07-17
            [newDate] => Array
                (
                    [0] => 2020-07-30
                    [1] => 2020-07-07
                    [2] => 2020-07-08
                    [3] => 2020-07-17
                )
        
        )
        

        我放行后,输出会变成这样。

        [Mon Jul  6 10:35:42 2020] Array
        (
            [coursedate] => 2020-07-17
            [newDate] => Array
                (
                    [0] => 2020-07-07
                    [1] => 2020-07-08
                    [2] => 2020-07-17
                    [3] => 2020-07-30
                )
        
        )
        

        已经排序的日期是最晚的日期。

        这是我们有对象的地方,然后我们要对所有对象进行排序

        例子:-

        [status_custom] => Array
            (
                [0] => stdClass Object
                    (
                        [type] => LDL - B2
                        [date] => 23/10/2014
                    )
        
                [1] => stdClass Object
                    (
                        [type] => LDL - D
                        [date] => 18/04/2015
                    )
           )
        

        首先我们必须转换。 Refer to this link to convert 然后我们对它们进行排序。 Refer to this link to sort object

        我们开始:-

            //At here we want to sort according to latest date. But this one involve with the object also.
            usort($array_custom_object, function ($a, $b) {
                $date1 = $a->date;
                $date2 = $b->date;
        
                //Since the income date is formatted like this d/m/Y , we have to change it
                $date1 = date_format(date_create_from_format('d/m/Y', $date1), 'Y-m-d');
                $date2 = date_format(date_create_from_format('d/m/Y', $date2), 'Y-m-d');
        
                if ($date1 > $date2) {
                    return -1;
                }
                if ($date1 == $date2) {
                    return 0;
                }
                if ($date1 < $date2) {
                    return 1;
                }
            });
            $dataFinal->status_custom = $array_custom_object; //Result
        

        【讨论】:

          【解决方案7】:

          使用 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);
          

          输出将是:

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

          【讨论】:

          • 如果您有 DateTime 或 Carbon 日期对象的列表,您的答案就很接近了。但是我相信你有反对票,因为你建议使用 asort() 而不是 sort(),如果用户使用 asort(),那么数字索引引用将不会反映被转储的实际排序列表。
          猜你喜欢
          • 2010-10-10
          • 1970-01-01
          • 1970-01-01
          • 2011-02-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-04
          相关资源
          最近更新 更多