php - 跟踪用户登录的时长

标签 php html session cookies time

我目前正在尝试弄清楚如何计算用户登录我网站的时间,然后显示时间。

本质上,当用户登录时,我需要一种方法来记录他们登录的时间,然后在我站点的另一个页面上,我想向他们展示他们已经登录了多长时间。我将如何去关于使用 cookie 或 session 变量执行此操作?

最佳答案

我会将他们登录的时间存储为 session 变量,例如

$_SESSION['loginTime'] = new DateTime(date('y-m-d h:m:s'));

然后用diff计算差值。

获取当前时间

$difference= $_SESSION['loginTime']->diff(new DateTime(date('y-m-d h:m:s')));

然后你可以用这些方法输出时间since

echo $difference->y; //return the difference in Year(s).
echo $difference->m; //return the difference in Month(s).
echo $difference->d; //return the difference in Day(s).
echo $difference->h; //return the difference in Hour(s).
echo $difference->i; //return the difference in Minute(s)
echo $difference->s; //return the difference in Second(s). 

关于php - 跟踪用户登录的时长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23439906/

相关文章:

html - 将文本对齐到按钮底部和背景前面

html - 在 Bootstrap 中没有滚动条时,无法获得 "col"背景色来覆盖整个区域

PHP:$_SESSION 作为数组不起作用,不允许我添加新项目?

php - 访问 apache 禁止的 laravel 存储访问上的文件

php - Json_encode() 在 PHP 5.1 中不起作用?

php - FFMpeg 在命令行中工作但不在 PHP 脚本中工作

php - Laravel验证数字多维数组

html - 将圆分成 3 部分,每个部分内有垂直文本

javascript - 当 session 结束时执行 sails.js 中的代码

session - Web 应用程序上下文中的 session 到底意味着什么