duanshanduo3363 2015-06-05 15:01
浏览 141
已采纳

按时间分组并显示它们

I have a task to create an activity feed view like https://dribbble.com/shots/1434168-Assembly-Activity-Stream/attachments/211032.

I want to group notes/posts in groups like 'an hour ago', '2 hours ago', '3 ..', etc. and display it on the page. (Like in image) For example my array looks like:

$data = array(
    array(
            'id' => 1,
            'msg' => '...',
            'created_at' => timestamp
        ),
    array(
            'id' => 2,
            'msg' => '...',
            'created_at' => timestamp
        ),
    ...
    ...
);

What is the best solution to do it?

  • 写回答

3条回答 默认 最新

  • dongshi4589 2015-06-05 15:35
    关注

    imo, you should fetch your data ordered by created_at DESC and then while looping through your array to compare the previous timestamp with the current (in the loop) if it's more than 1h difference it should go in another group.

    I'm using Carbon which is available in Laravel 4.2.

    A basic example (I believe I've not covered all of the cases, but it's a start. Left some comments and debug messages for clearance:

    $activityPosts = [
        [
            'id' => null,
            'msg' => 'Message #',
            'created_at' => strtotime('yesterday 20:00'),
        ],
        [
            'id' => null,
            'msg' => 'Message #',
            'created_at' => strtotime('yesterday 19:37'),
        ],
        [
            'id' => null,
            'msg' => 'Message #',
            'created_at' => strtotime('yesterday 19:29'),
        ],
        [
            'id' => null,
            'msg' => 'Message #',
            'created_at' => strtotime('yesterday 19:13'),
        ],
        [
            'id' => null,
            'msg' => 'Message #',
            'created_at' => strtotime('yesterday 18:25'),
        ],
        [
            'id' => null,
            'msg' => 'Message #',
            'created_at' => strtotime('yesterday 18:01'),
        ],
        [
            'id' => null,
            'msg' => 'Message #',
            'created_at' => strtotime('yesterday 13:56'),
        ],
        [
            'id' => null,
            'msg' => 'Message #',
            'created_at' => strtotime('yesterday 12:18'),
        ],
        [
            'id' => null,
            'msg' => 'Message #',
            'created_at' => strtotime('yesterday 12:05'),
        ],
        [
            'id' => null,
            'msg' => 'Message #',
            'created_at' => strtotime('yesterday 10:28'),
        ]
    ];
    $activityPosts = array_reverse($activityPosts); //I just built the array wrong (Im not a smart girl...)
    echo '<div style="border: 1px solid red; width: 250px; margin-bottom: 5px">';
    foreach ($activityPosts as $k => $post) {
        $getHour = \Carbon\Carbon::createFromTimeStamp($post['created_at'])->hour;
        if (isset($activityPosts[$k - 1])) {
            $prevHour = \Carbon\Carbon::createFromTimeStamp($activityPosts[$k - 1]['created_at'])->hour;
            if ($getHour !== $prevHour) {
                echo '</div>';
                echo '<div style="border: 1px solid red; width: 250px;margin-bottom: 5px">';
            }
            echo '<hr />';
            echo 'Current: ' . $getHour . 'h Prev:' . $prevHour . 'h';
            echo '<hr />';
        }
        echo "<h4>Message: {$post['msg']}{$k}</h4>";
        echo $post['created_at'];
        echo '<h4>' . \Carbon\Carbon::createFromTimeStamp($post['created_at'])->diffForHumans() . '</h4>';
    }
    echo '</div>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序