PHP - 根据日期对数组进行分组

时间:2016-05-09 15:03:58

标签: php arrays sorting datetime

我有这样的数组:

$events = [

[
  'title' => 'Event 1',
  'description' => 'Some Text',
  'date_added' => 2016-05-06 14:57:39
],

[
  'title' => 'Event 2',
  'description' => 'Some Text',
  'date_added' => 2016-05-08 14:57:39
],
[
  'title' => 'Event 3',
  'description' => 'Some Text',
  'date_added' => 2016-05-09 14:57:39
],
[
  'title' => 'Event 4',
  'description' => 'Some Text',
  'date_added' => 2016-05-09 15:57:39
]

];

我需要显示这样的数据:

**Today events:**
Event 4,
Event 3

**Yesterday events**
Event 2

**May 6th events**
Event 1

我该怎么做?有可能吗?

3 个答案:

答案 0 :(得分:1)

这完全符合您的要求。试试这个:

首先,我们需要明确地对记录进行排序。我们可以使用usort()函数。

function compare_date($a, $b) {

    if ($a['date_added'] === $b['date_added']){
        return 0;
    }

    return ($a['date_added'] > $b['date_added']) ? -1 : 1;
}

usort($events, "compare_date");

接下来,我们正在创建一个日期保存标题的自定义数组。

foreach($events as $event) {

    $today_date =   strtotime(date("Y-m-d H:i:s"));
    $event_date =   strtotime($event['date_added']);
    $diff       =   floor(($today_date - $event_date)/(60*60*24));

    switch($diff) {
        case 0:
            $title             = "Today events:<br/>";
            $result[$title][]  = $event['title'];
            break;
        case 1:
            $title             =  "Yesterday events:<br/>";
            $result[$title][]  =  $event['title'];
            break;
        default:
            $title             = date("M jS", $event_date)." Events:<br/>";
            $result[$title][]  = $event['title'];
    }
}

最后,我们遍历这个$ result并显示所需的输出。

foreach($result as $k => $rslt) {

    echo $k;
    echo implode("<br>", $rslt);
    echo "<br/>";
}

答案 1 :(得分:0)

您可以使用usort():

执行此操作
function date_compare($a, $b) {
    $t1 = strtotime($a['date_added']);
    $t2 = strtotime($b['date_added']);
    return $t1 - $t2;
}    
usort($events, 'date_compare');

要显示它们,您可以使用以下foreach语句:

foreach ($events as $key) {
    $dt = new DateTime($key['date_added']);
    $date =  $dt->format('F j');
    if($current != $date) {
        $current = $date;
        echo "<br>**".$date." events:**<br>";
    }
    echo $key["title"]."<br>";
}

输出:

**May 1 events:**
Event 3

**May 3 events:**
Event 1

**May 8 events:**
Event 2

**May 9 events:**
Event 4

希望这个解决方案有效。祝你一切顺利。

答案 2 :(得分:0)

你也可以试试这个:

// method to compare two values
function compare_date($a, $b) {

    if ($a['date_added'] === $b['date_added']){
        return 0;
    }

    return ($a['date_added'] > $b['date_added']) ? -1 : 1;
}

// callback function
usort($events, "compare_date");

$today = date("Y-m-d");
$yesterday = date('Y-m-d', strtotime("yesterday"));

$myEvents = array();

foreach ($events as $key => $val) : 
    $datetime = new DateTime($val['date_added']);
    $record_date = $datetime->format('Y-m-d');

    if ($today == $record_date):
        $myEvents['today'][] = $events[$key];
    elseif ($yesterday == $record_date):
        $myEvents['yesterday'][] = $events[$key];
    else :
        $myEvents[$record_date][] = $events[$key];
    endif;  
endforeach;

// display event
foreach($myEvents as $i => $v):
    if ($i!="today" && $i!="yesterday"):
        echo date('M d', strtotime($i)) . "<br>";       
    else :
        echo $i . "<br>";
    endif;

    foreach ($v as $e):
        echo $e['title'] . "<br>";
    endforeach;
    echo "<br>";
endforeach;

以上代码的输出是:

<强>今天

活动4

活动3

<强>昨天

活动2

5月06日

活动1