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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)