user2058653
user2058653

Reputation: 733

Group by time groups and display them

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?

Upvotes: 1

Views: 111

Answers (3)

Ivanka Todorova
Ivanka Todorova

Reputation: 10219

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>';

Upvotes: 1

Bishal Paudel
Bishal Paudel

Reputation: 1946

Use Carbon

example:

echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'

Upvotes: 2

Alex
Alex

Reputation: 626

You can create a function to do so :

function dateDifference($first,$last){
    $hourdiff = round((strtotime($last) - strtotime($first))/3600);
    return $hourdiff;
}

$dt1= "2015-06-05 11:07:23";
$dt2= date("Y-m-d H:i:s");
echo dateDifference($dt1,$dt2);

Upvotes: 2

Related Questions