Group items in PHP foreach loop, if timestamp is with set time limit

I have an ativity feed as part of my web application and would like to group similar results (ie those that are by the same user for the same action like a profile update or photo upload) into one result that would say something 'user x has added 4 photos) rather than 4 indivudual items.

Is there a way to achieve this in PHP?

The activity feed is created using a foreach loop that loops through resullts from a dedicated users_activity table in my database.

Screenshot_3

Each row of the activity table contains the user ID as well as a timestamp.

I’d try posting this on Stack Overflow, if you haven’t done it yet!