I'm currently writing a snippet of code which will look through the database and see if any admins are currently online and show them on the side of the page as being online.
Every time a user browses through a page on my site (when they are logged in) it grabs the current time and updates a column called lastseen
.
I have a role column that marks a user from an admin and what I want it to do is query the database for all the admins and check the lastseen
column and if that time is within 5 minutes of the current time then output them to a row which I can then take and format later.
This is what I currently tried but nothing came back.
SELECT username, role, lastseen
FROM database.accounts
WHERE lastseen >= DATE_SUB(NOW(), INTERVAL 10 MINUTE) AND role='admin'
I am storing lastseen
as date('Y-m-d H-i-s')
from PHP so it looks like this
2017-02-18 16:07:42
in the database.
Using the getMinutes() and setMinutes() Methods In this approach, we will create a new date object and extract the minutes from that using the getMinutes() method. After that, we will update the minutes by subtracting the minutes from the minutes we got from the date.
To subtract time, type in =B2-B1, and it'll return the elapsed time. The answer is displayed as an AM time, so to change that, right click and select Format Cells and change it to h:mm. It'll return the answer in time format (7:35, or 7 hours and 35 minutes).
In cell D2, subtract the end time from the start time by entering the formula =C2-B2, and then press Enter. In the Format Cells box, click Custom in the Category list. In the Type list, click h:mm (for hours and minutes), and then click OK.
What is your datatype for lastseen
, Your query seems correct , just try with
SELECT username, role, lastseen FROM database.accounts
WHERE STR_TO_DATE(lastseen, '%Y-%m-%d %H:%i:%s') >= DATE_SUB(NOW(), INTERVAL 10 MINUTE) AND role='admin'
Also make sure your PHP and MYSQL Server have same timezones
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With