dongqi7631 2016-06-23 13:08
浏览 14
已采纳

如何创建计算上次看到的时间的系统

I'm trying to show my website's users a time/date (which determines the last time of their visit of my website) in their profile page. Something exactly like this:

enter image description here

Well how can I calculate that number (8 min ago) ?


Currently I have a table like this:

// requests
+----+----------+-------------+
| id | id_user  |  unix_time  |
+----+----------+-------------+
| 1  | 2353     | 1339412843  |
| 2  | 2353     | 1339412864  |
| 3  | 5462     | 1339412894  |
| 4  | 3422     | 1339412899  |
| 5  | 3422     | 1339412906  |
| 6  | 2353     | 1339412906  |
| 7  | 7785     | 1339412951  |
| 8  | 2353     | 1339413640  |
| 9  | 5462     | 1339413621  |
| 10 | 5462     | 1339414490  |
| 11 | 2353     | 1339414923  |
| 12 | 2353     | 1339419901  |
| 13 | 8007     | 1339424860  |
| 14 | 7785     | 1339424822  |
| 15 | 2353     | 1339424902  |
+----+----------+-------------+

As you see table above stores all my website's requests. I mean I insert a new row into that table when user opens (loads) any page of my website. And then to calculate last_seen, I simply select last row for that specific user and use him unix_time value.

To be honest, I guess what I'm doing is wrong (or at least isn't standard). Because there is a lot of costs to calculate just a last_seen.

Also some days ago, I saw a file named access_log.txt in the server which was containing all requests. Now I want to know can I use that file for doing that? Also would it be better than my current approach (using database) ? In total, how can I calculate last_seen for each user?

  • 写回答

2条回答 默认 最新

  • dtpfia3334 2016-06-23 13:11
    关注

    Just use Datetime::diff, get the timestamp from the last entry in your database for user X. Then create 2 datetime objects, the first one with your old code and the second one with the current time. Then call diff and hand in the current timestamp.

    $datetime1 = new DateTime(date('Y-m-d H:i:s', 1339412843));
    $datetime2 = new DateTime(date('Y-m-d H:i:s'));
    
    $diff = $datetime1->diff($datetime2);
    
    echo $diff->y.' Years <br/>';
    echo $diff->m.' Months <br/>';
    echo $diff->d.' Days <br/>';
    echo $diff->h.' Hours <br/>';
    echo $diff->i.' Minutes <br/>';
    echo $diff->s.' Seconds <br/>';
    

    Appendix: Your current approach is correct. Calculating the time diff in mysql is not as costly as parsing the access logs. Using sql this is 1 query execution, 2 objects to be created and at least 1 function call.

    If you want to use access logs you always have to parse the full access log. Parse all lines and then filter out unrelated lines and only keep the last on in which the user logged in. If your apache has already rotated the logs you will also have to search in other logs that you also have to uncompress.

    I would keep your current strategy and try to optimize it e.g.

    • Using joins to get all users and all "last" timestamps at once that are visible on the current page.
    • Do not include "seconds seen before" so you could use some kind of caching.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)