php 如何根据日期排序数组?

dzhpxtsq  于 5个月前  发布在  PHP
关注(0)|答案(9)|浏览(84)

我有以下数组

$dates = array(
    '2015-02-13',
    '2015-04-21',
    '2015-08-18',
    '2015-11-26',
    '2015-09-15',
    '2015-01-07',
    '2015-02-11',
    '2015-07-14',
    '2015-03-02',
);

字符串
我想从最早到最近的日期对这个数组进行排序。

foreach ($dates as $date) {
    $date = new DateTime($date);
    // The logic I need
}


你能帮帮我吗?

编辑

我想指出的是,日期格式可以有所不同。为了详细说明,我在我的数据库中的日期格式为YYYY-MM-DDYYYY/MM/DDDD-MM-YYYYDD/MM/YYYY
但正如下面所建议的,我可以重新格式化日期并构建一个新数组。所以这可能不是问题。

wfauudbj

wfauudbj1#

php在MM DD YYYY下不能很好地工作,因为daymonth之间的混淆。
对于第一个和第二个条件YYYY-MM-DDYYYY/MM/DD,您可以遵循以下解决方案:

$dates = array(
    '2015-02-13',
    '2015-04-21',
    '2015-08-18',
    '2015-11-26',
    '2015-09-15',
    '2015-01-07',
    '2015-02-11',
    '2015-07-14',
    '2015-03-02',
);

function date_sorter($a, $b)
{
    $t1 = strtotime($a);
    $t2 = strtotime($b);
    return $t1 - $t2;
}
usort($dates, 'date_sorter');

// check the output
echo '<pre>'.print_r($dates,1).'</pre>'

字符串
输出值:

Array
(
[0] => 2015-01-07
[1] => 2015-02-11
[2] => 2015-02-13
[3] => 2015-03-02
[4] => 2015-04-21
[5] => 2015-07-14
[6] => 2015-08-18
[7] => 2015-09-15
[8] => 2015-11-26
)


FYI:您可以尝试将-更改为/
如果格式类似于YYYY DD MM(无论是/还是-),您可以看到以下解决方案:
样品输入:

$dates = array(
   '13/02/2015',
   '21/04/2015',
   '18/08/2015',
   '26/11/2015',
   '15/09/2015',
   '07/01/2015',
   '11/02/2015',
   '14/07/2015',
   '02/03/2015',
);


功能说明:

function date_sorter($a, $b )
{
   $da = DateTime::createFromFormat('d/m/Y', $a); // define date format d/m/Y
   $db = DateTime::createFromFormat('d/m/Y', $b);  // define date format d/m/Y

   $t1 = strtotime($da->format('Y-m-d'));
   $t2 = strtotime($db->format('Y-m-d'));
   return $t1 - $t2;
}
usort($dates, 'date_sorter');

echo '<pre>'.print_r($dates,1).'</pre>';


输出量:

Array
(
   [0] => 07/01/2015
   [1] => 11/02/2015
   [2] => 13/02/2015
   [3] => 02/03/2015
   [4] => 21/04/2015
   [5] => 14/07/2015
   [6] => 18/08/2015
   [7] => 15/09/2015
   [8] => 26/11/2015
)

wsewodh2

wsewodh22#

根据您的日期格式(在您的情况下,可以使用Y-m-d),只需执行排序

sort($dates);

字符串

j91ykkif

j91ykkif3#

您拥有的日期是缩短的ISO 8601格式,即YYYY-MM-DD
它们有一个很好的特性,即词法排序顺序与日期顺序相同。所以,只需使用默认的排序函数对数组进行排序。它就可以工作了。
当在代码中处理日期字符串时,最好使用ISO 8601作为规范的内部日期格式。尽快将用户提供的格式转换为ISO 8601,如果用户需要在自己的语言环境中输出,则尽可能推迟。当然,使用“适当”的日期对象更好!

dgtucam1

dgtucam14#

只需尝试将所有日期格式转换为时间数字。

<?php
$finalArray = array();
foreach ($datesInFormatA as $date) {
    $finalArray[] = strtotime($date);
}
foreach ($datesInFormatB as $date) {
    $finalArray[] = strtotime($date);
}

字符串
对于所有的日期格式都这样做,然后你就有了一个数字数组,你可以很容易地对它们进行排序。

$finalArray = sort($finalArray);

vlurs2pr

vlurs2pr5#

您可以使用strtotime()进行转换以安全地进行日期操作,然后可以重新排序数组:

$dates = array(
    '2015-02-13',
    '2015-04-21',
    '2015-08-18',
    '2015-11-26',
    '2015-09-15',
    '2015-01-07',
    '2015-02-11',
    '2015-07-14',
    '2015-03-02',
);
$newArray = array();

foreach($dates as $value) {
    $newArray[] = strtotime($value);
}

sort($newArray);

var_dump($newArray);

字符串

ssgvzors

ssgvzors6#

使用PHP sort函数

<?php
$dates = array(
    '2015-02-13',
    '2015-04-21',
    '2015-08-18',
    '2015-11-26',
    '2015-09-15',
    '2015-01-07',
    '2015-02-11',
    '2015-07-14',
    '2015-03-02',
);

sort($dates);

var_dump($dates);

字符串
输出量:

array(9) {
  [0]=>
  string(10) "2015-01-07"
  [1]=>
  string(10) "2015-02-11"
  [2]=>
  string(10) "2015-02-13"
  [3]=>
  string(10) "2015-03-02"
  [4]=>
  string(10) "2015-04-21"
  [5]=>
  string(10) "2015-07-14"
  [6]=>
  string(10) "2015-08-18"
  [7]=>
  string(10) "2015-09-15"
  [8]=>
  string(10) "2015-11-26"
}


请记住,这适用于'Y-m-d'格式。对于其他格式,您需要使用strtotime()函数转换为日期格式并继续进行。类似于这样的(标准格式):

function sortFunction( $a, $b ) {
    return strtotime($a[0]) - strtotime($b[0]);
}
usort($dates, "sortFunction");


m/d/y或d-m-y格式的日期通过查看各个组件之间的分隔符来消除歧义:如果分隔符是斜线(/),则假定为美国m/d/y格式;而如果分隔符是破折号(-)或点(.),则假定为欧洲d-m-y格式。

6yjfywim

6yjfywim7#

试试这个快速排序算法。它所做的是,它将日期与从Jan 01 1970 (UTC))开始的Unix Timestamp进行比较,然后将其排序并放入数组中。
转换为Unix Timestamp确保ISO8601日期格式。

$dates = array(
    '2015-02-13',
    '2015-04-21',
    '2015-08-18',
    '2015-11-26',
    '2015-09-15',
    '2015-01-07',
    '2015-02-11',
    '2015-07-14',
    '2015-03-02',
);

function quick_sort($array)
{
    // find array size
    $length = count($array);

    // base case test, if array of length 0 then just return array to caller
    if($length <= 1){
        return $array;
    }
    else{

        // select an item to act as our pivot point, since list is unsorted first position is easiest
        $pivot = $array[0];

        // declare our two arrays to act as partitions
        $left = $right = array();

        // loop and compare each item in the array to the pivot value, place item in appropriate partition
        for($i = 1; $i < count($array); $i++)
        {
            if(strtotime($array[$i]) < strtotime($pivot)){
                $left[] = $array[$i];
            }
            else{
                $right[] = $array[$i];
            }
        }

        // use recursion to now sort the left and right lists
        return array_merge(quick_sort($left), array($pivot), quick_sort($right));
    }
}

$sorted = quick_sort($dates);
print_r($sorted);

字符串
//输出

Array
(
    [0] 2015-01-07
    [1] 2015-02-11
    [2] 2015-02-13
    [3] 2015-03-02
    [4] 2015-04-21
    [5] 2015-07-14
    [6] 2015-08-18
    [7] 2015-09-15
    [8] 2015-11-26
)

uemypmqf

uemypmqf8#

我有一个解决方案给你,希望这将有助于你排序,因为这是为我工作。
我建议你使用usort()与自定义函数:

$dates = array(
    '2015-02-13',
    '2015-04-21',
    '2015-08-18',
    '2015-11-26',
    '2015-09-15',
    '2015-01-07',
    '2015-02-11',
    '2015-07-14',
    '2015-03-02',
);

function date_fun_compare($a, $b)
    {
        $t1 = strtotime($a['datetime']);
        $t2 = strtotime($b['datetime']);
        return $t1 - $t2;
    }    
    usort($dates, 'date_fun_compare');

字符串
希望这对你有帮助。

knpiaxh1

knpiaxh19#

好吧,既然你提到你所有的日期都有以下格式:YYYY-MM-DD,YYYY/MM/DD,DD-MM-YYYY和DD/MM/YYYY
首先,你需要将它们转换成一种格式,让我们使用YYYY-MM-DD。然后,正如已经提到的,你只需要常规排序。
包括如何重新格式化字符串,而不必为每个字符串创建一个日期对象。

$dates = array(
        '2015-02-13',
        '2015-04-21',
        '2015-08-18',
        '2015-11-26',
        '2015-09-15',
        '2015/01/07',
        '2015-02-11',
        '2015/07/14',
        '2015-03-02',
        '08/01/2015',
        '08-04-2015',
    );

array_walk($dates, function(&$el){
    if($el[4] !== '-' && $el[4] !== '/'){
        // Ugly, I know, but is the fastest
        $el = $el[6] . $el[7] .$el[8] . $el[9] . '-' . $el[3] . $el[4] . '-' . $el[0] . $el[1]; 
        // Or just explode, revert array and implode
    }elseif($el[4] !== '-'){
        $el = str_replace('/', '-', $el);
    }
});
sort($dates);
print_r($dates);

字符串
将打印:

Array
(
    [0] => 2015-01-07
    [1] => 2015-01-08
    [2] => 2015-02-11
    [3] => 2015-02-13
    [4] => 2015-03-02
    [5] => 2015-04-08
    [6] => 2015-04-21
    [7] => 2015-07-14
    [8] => 2015-08-18
    [9] => 2015-09-15
    [10] => 2015-11-26
)


如果你想换个方式,就用rsort($dates)吧。

相关问题