0

php中有没有一种方法可以按日期对数组进行排序并显示它们?类似于 asort 或 usort?

问题是日期字段也可以包含“未知”之类的值。我想按日期降序打印所有记录,最后打印 UNKNOWNS。到目前为止,这是我的代码。

foreach ($MailPieceArr as $key => $val) {

    $count++;
    if (($count % 2) == 1) {
        print "<tr bgcolor='#CCCCCC'>";
    } else {
        print "<tr bgcolor=white>";
    }

    print "<td align=center>".$MailPieceArr[$key]["type"]."</td>";
    print "<td align=center>".$MailPieceArr[$key]["id"]."</td>";
    print "<td align=center>".$MailPieceArr[$key]["ddate"]."</td>";
    print "<td align=center>".$MailPieceArr[$key]["acode"]."</td>";

    print "</tr>";
}

这是我的数组的样子:

print_r($MailPieceArr);

输出:

Array
(
    [0] => Array
        (
            [type] => First-Class
            [id] => 77770056
            [ddate] => 11/26/2012
            [acode] => 2643
        )

    [1] => Array
        (
            [type] => First-Class
            [id] => 77770057
            [ddate] => 11/26/2012
            [acode] => 2643
        )

    [2] => Array
        (
            [type] => First-Class
            [id] => 77770058
            [ddate] => 11/21/2012
            [acode] => 2641
        )

    [3] => Array
        (
            [type] => First-Class
            [id] => 77770059
            [ddate] => 11/21/2012
            [acode] => 2641
        )

    [4] => Array
        (
            [type] => First-Class
            [id] => 77770042
            [ddate] => 10/29/2012
            [acode] => 2623
        )

    [5] => Array
        (
            [type] => First-Class
            [id] => 77770041
            [ddate] => 10/29/2012
            [acode] => 2623
        )

    [6] => Array
        (
            [type] => RR
            [id] => 88880001
            [ddate] => 10/29/2012
            [acode] => 2621
        )

    [7] => Array
        (
            [type] => First-Class
            [id] => 77770043
            [ddate] => 10/17/2012
            [acode] => 2625
        )

    [8] => Array
        (
            [type] => First-Class
            [id] => 77770044
            [ddate] => 10/17/2012
            [acode] => 2625
        )

    [9] => Array
        (
            [type] => RR
            [id] => 88880005
            [ddate] => 10/17/2012
            [acode] => 2619
        )

    [10] => Array
        (
            [type] => IMB
            [id] => 0001101111111111110011111111111
            [ddate] => UNKNOWN   
            [acode] => 2643
        )

)
4

3 回答 3

1

在您的排序功能中使用strtotime,不仅更容易比较时间戳,而且在无效日期格式上也返回 false。

function date_sort($a, $b)
{
    $t1 = strtotime($a['ddate']);
    $t2 = strtotime($b['ddate']);

    if($t1 == false || $t2 == false)
        return 1;

    return $t2 - $t1;
}    
usort($array, 'date_sort');

这可以帮助您生成类似此示例的内容。

于 2012-11-26T23:43:20.280 回答
0

Usingusort()将允许您使用完全自定义的比较函数对数组进行排序:

<?php
date_default_timezone_set('UTC');

$array  = array(
    array(
        'type'  => 'First-Class',
        'id'    => 7770057,
        'ddate' => '2012/11/26',
        'acode' => 2643
    ),
    array(
        'type'  => 'First-Class',
        'id'    => 7770058,
        'ddate' => '2012/11/21',
        'acode' => 2643
    ),
    array(
        'type'  => 'First-Class',
        'id'    => 7770059,
        'ddate' => 'UNKNOWN',
        'acode' => 2643
    ),
    array(
        'type'  => 'First-Class',
        'id'    => 7770060,
        'ddate' => 'UNKNOWN',
        'acode' => 2643
    ),
    array(
        'type'  => 'First-Class',
        'id'    => 7770061,
        'ddate' => '2012/11/30',
        'acode' => 2643
    ),
);


echo 'Before:'.PHP_EOL;
foreach($array as $item){
    echo $item['id'].': '.$item['ddate'].PHP_EOL;
}

usort($array, function($a, $b){
    if($a['ddate'] == 'UNKNOWN'){
            // Place UNKNOWN at end
        return 1;
    }

    if($a['ddate'] == $b['ddate']){
            // Dates are the same
        return 0;
    }
    return (strtotime($a['ddate']) > strtotime($b['ddate'])) ? -1 : 1;
});

echo 'After:'.PHP_EOL;
foreach($array as $item){
    echo $item['id'].': '.$item['ddate'].PHP_EOL;
}

但是,您应该考虑使用 PHP 的DateTime 类来存储您的日期对象。使用字符串可能会产生歧义——例如,2012 年 11 月 12 日是 12 月 11 日还是 11 月 12 日?

于 2012-11-26T23:43:52.167 回答
0

您可以将日期转换为 unix 时间戳,它们将更容易比较。

于 2012-11-26T23:33:28.293 回答