简体   繁体   English

PHP时间戳记,今天向用户显示在线

[英]PHP timestamp, show users online today

I had a function working which showed me users that have visited my website today, there is a field in a MYSQL database called 'lastvisit'. 我的某个功能正常运行,向用户显示今天访问过我的网站的用户,在MYSQL数据库中有一个名为“ lastvisit”的字段。

I use to use a MYSQL time stamp however server time was different to my local time, so didn't work properly. 我曾经使用MYSQL时间戳,但是服务器时间与本地时间不同,因此无法正常工作。

I know set the time zone in PHP and store the date and time as a VARCHAR in the database, like: 我知道在PHP中设置时区并将日期和时间作为VARCHAR存储在数据库中,例如:

date_default_timezone_set('UTC');
$visittime = date('jS M Y H:i A');

//Update timestamp to show latest visit
$sql="UPDATE users SET lastvisit='$visittime' where email ='$email' ";


29th Jan 2013 22:29 

How can I write a PHP function to show users that have been online today, and the amount of users online within the last 24 hours. 如何编写PHP函数以显示今天已在线的用户以及最近24小时内的在线用户数量。

This will do the job if you will be doing it in MySQL assuming that you want to count distinct email . 如果您要在MySQL进行此操作(假设您要计算不同的email ,则可以完成此工作。 ( much better if you can use the primary key of the user. ) 如果可以使用用户的主键,那就更好了。

This will return total count of users online yesterday and today. 这将返回昨天和今天在线的用户总数。

SELECT  DATE(STR_TO_DATE(lastvisit, '%D %b %Y %H:%i')), 
        COUNT(DISTINCT email)
FROM    users
WHERE   DATE(STR_TO_DATE(lastvisit, '%D %b %Y %H:%i')) BETWEEN CURDATE + INTERVAL -1 DAY AND CURDATE()
GROUP   BY DATE(STR_TO_DATE(lastvisit, '%D %b %Y %H:%i'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM