I'm working on a small project and I've run into some trouble.
The issue is that I want to check if there has been more than 30 minutes since a u_latest_activity latest was updated, if there's been, then update and set u_online=0
The field in the MySQL database is a datetime field.
I have no PHP code to show yet since none that I've tried has worked, I tried using the DateTime object but I'm clueless about how it works. I googled alot but I still couldn't figure it out so I use this as a last resort. I'll gladly accept a SQL solution as well.
Thanks in advance.
This should do it:
UPDATE your_table SET u_online = 0 WHERE u_latest_activity < DATE_SUB(NOW(), INTERVAL 30 MINUTE)
this is pure mySQL ans might help
SELECT if(DATE_ADD(u_latest_activity, INTERVAL 30 MINUTE) > NOW() , 1 , 0) as u_online from mytable;
Related
I'm using MYSQL 5.5.47 on Debian and I have this bit in my query:
AND DATEDIFF(CURRENT_DATE(), shouts_stores.date) <= 7
-- changed it from AND DATEDIFF(NOW(), shouts_stores.date) <= 7
The problem?
Datediff, according to everything I've read, is supposed to ignore the time part.
I have to get rows of the last 7 days, but I need the timestamps with a time element for "Posted X seconds/minutes/hours/days ago" functionality in PHP.
However, when it comes to just getting the rows, I simply need the posts from up to (and including) 7 days (or 1 week) ago. However, if the posting time is earlier than it is right now, it'll knock it up to 8 days. This causes some confusion. Does anyone have any idea about this?
edit: example
This is what the table looks like:
tl dr; I need DATEDIFF() to ignore the time part of the datetime.
Try:
... DATEDIFF(CURRENT_DATE(), DATE(shouts_stores.date)) <= 7
Try this:
DATE_ADD(shouts_stores.date,INTERVAL 7 DAY) >= CURRENT_DATE()
replace CURRENT_DATE() with NOW() as needed
How can I add two hours to the mysql field of TIME type? This problem seems unsolvable to me. I have tried:
1)
select TimeStart + interval 2 hour
from table
returns null
2)
select addtime(TimeStart, interval 2 hour)
from table
returns error
3)
select convert_tz(TimeStart, 'GMT', 'CEST')
from table
(because the 2 hour difference was due to messed up timezones, so this is another way to correct it)
also returns null!
4)
select date_add(TimeStart, interval 2 hour)
from table
also returns null!
I start to curse mysql... would be easy with DATETIME type, but really not very easy to work with the TIME type.
Sorry I just figured it out!
addtime(TimeStart, '2:00')
Not very intuitive and consistent with the INTERVAL thing though!
SELECT HOUR(TimeStart)+2 FROM table
gives you the 2 hour increment.
Hi I have have a column in my mysql db table called FirstAdded -- the date and is stored in this format:2013-03-04 16:37:05
I would like to pull all the rows added to the database in the past hour based on the FirstAdded column.
I've read around about INTERVAL but don't really know here to start
Can someone please help?
Thanks very much,
This should get you started
SELECT *
FROM Tablename
WHERE FirstAdded > CURRENT_TIMESTAMP - INTERVAL 1 HOUR
I am trying to update a DATETIME field in a table. What I want for it is to always have three hours before it expires.
My current code is:
UPDATE mindcore_sessions
SET session_time = DATE_ADD(session_time, INTERVAL 3 HOUR)
WHERE session_id = '$sessionId';
An example value would be: 2013-02-11 00:00:00. If I run this query, it will change to: 2013-02-11 03:00:00. Which is correct. But, if it is run again, it changes to 2013-02-11 06:00:00 and so on.
What I want for it is to always be only three hours ahead. This is hard to explain, but I just want it to stay the same if it is run again. And if it is run again 1 minute later, I want it to increment by just one minute.
Sorry if the question is verbose, but I am utterly confused (and it is late!).
Thanks :)
Instead of adding three(3) hours to the last session_time, add three(3) hours to NOW():
update mindcore_sessions
set session_time = DATE_ADD(NOW(), INTERVAL 3 HOUR)
where session_id = '$sessionId';`
Add your interval to NOW() instead of adding to the current value:
UPDATE mindcore_sessions SET session_time = DATE_ADD(NOW(), INTERVAL 3 HOUR) WHERE session_id = '$sessionId';
There is no way in mysql to know if the same query was executed before.
The only possible solution would be to save the original value in an extra column, and use this column to check if the value was changed in the past.
If you want the value to be ahead 3 hours from now, you can simply do this:
DATE_ADD(NOW(), INTERVAL 3 HOUR)
I'm trying to get all the records just for today but having trouble. Another thing I'm unsure of how to factor in is that my server time is two hours ahead of my local time so I'll need to figure out the offset. I'm off setting the time when I'm inserting the data just not sure how to do it on retrieval of if I'll need to.
mysql_query("SELECT `* FROM table WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= `date` AND `alert_status` ='0'") or die(mysql_error());
I think part of the problem is the <= I tried just using = and == but neither worked.
What exactly is the problem you're running into? What do you mean when you say that it doesn't work?
I see a syntax error (the backtick right before the asterisk), but I'm not sure what your issue is. That DATE_SUB call looks reasonable to me.
Edit: try something like this:
WHERE `date` BETWEEN DATE_SUB(CURDATE(),INTERVAL 1 DAY) AND CURDATE()
...which is just a fancy way of doing this:
WHERE `date` >= DATE_SUB(CURDATE(),INTERVAL 1 DAY) AND `date` <= CURDATE()