从mysql获取24小时活动(时间跟踪)

We have to track the time some users (employees) are spending on our Website (Backend Tool, they are logged in).

Currently we are saving an info into a MySQL Table every 5 minutes (based on activity (page open) or showing/reading a page (through an ajax updater, fired every 5 minutes).

In MySQL we store the user ID and a MySQL timestamp.

+----+---------------------+
| id |      timestamp      |
+----+---------------------+
| 28 | 2016-11-25 15:51:25 |
| 28 | 2016-11-25 15:55:00 |
| 28 | 2016-11-25 16:00:00 |
| 28 | 2016-11-25 16:05:00 |
| 28 | 2016-11-25 16:10:00 |
| 28 | 2016-11-25 16:15:00 |
| 28 | 2016-11-25 16:55:00 |
| 28 | 2016-11-25 17:00:00 |
| 28 | 2016-11-25 17:05:00 |
| 28 | 2016-11-25 17:10:00 |
| 28 | 2016-11-25 17:15:00 |
| 28 | 2016-11-25 17:20:00 |
| 28 | 2016-11-25 17:25:00 |
+----+---------------------+

What would be your suggestion to retrieve an "activity protocol" for a given user in a given time period.

i.e. "show all times" where a user has been active yesterday, last week, last month"

The result for Nov. 25th for user 28 should give an result like:

2016-11-25 -> 49 minutes
15:51-16:15 (24 min) 
17:00-17:25 (25 min)

Selecting those times from the DB is not the problem, but how can we combine those to an intelligent format...

yesterday: 8 hours 15 minutes, lastweek: 52 hours 35 minutes, last month: 175 hours 45 minutes

Also a "hint" on how to get a nice graphical representation would be very helpful:

Weelky "hour" report

One approach can be to divide the time list into ranges. Each range will represent the period of time during which the user was active. Next we need to find the time span for each range. The time span can be calculated by finding the difference between the highest and lowest times in a range. We can then add the time spans for all the ranges. This will give the amount of time the user was active.