Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtract 5 minutes from a date value

Tags:

mysql

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.

like image 721
Francis Booth Avatar asked Feb 18 '17 21:02

Francis Booth


People also ask

How do you subtract minutes from a date?

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.

How do I subtract 5 minutes from a time in Excel?

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).

How do I deduct minutes in Excel?

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.


1 Answers

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

like image 111
sumit Avatar answered Oct 19 '22 23:10

sumit