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()
Related
I tried every possible bracket combination but I just can't get this line to work!
I also tried looking for the solution but I can't seem to find anything similar. I hope someone can shed some light on my ignorance ^^.
UPDATE contact_info SET birthday= CURDATE() - TIME_TO_SEC(NOW()) % 50000 WHERE contactID=1;
Thanks in advance!
You need to convert the expression you calculate to an INTERVAL specifying the number of days. Then use DATE_SUB to subtract the interval from the current date.
UPDATE contact_info
SET birthday = DATE_SUB(CURDATE(), INTERVAL (TIME_TO_SEC(NOW()) % 50000) DAY)
WHERE contactID = 1;
Hi I am comparing two dates using mysql between function. My query looks like
select count(id) as total
from table
where user_id=111
and date_column BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY)
and NOW()
In between part of this query it is BETWEEN upperdate and lowerdate. It is working fine. But I went to verify this function on mysql documentation https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between . It says it should be
`BETWEEN LOWER AND UPPER`
Current I am doing in reverse and it is working fine but I just want to verify that is it rite and it will cause in problem in future and any hidden case.
It is working. Because:
DATE_SUB(NOW(), INTERVAL 1 DAY) -- a day less than now is yesterday
is LOWER THAN
NOW() -- it is now, means today with current time
So comparison would be like
between yes'day and today
which is valid
BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) and NOW()
is in other words
BETWEEN yesterday AND today
So it is
BETWEEN lower AND upper
No wonder it's working :)
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;
I was working and my friend told me to use curdate() on mysql query to get the current date of the server... And I told him that I was using Time_Stamp field for date/time.
Now I start to think, is there a huge difference between this two ways ? One is better than the other? Or there is something that makes it a not good practice ? Also there is a now() that can be used too. I just wanted to understand how does it work or wich one is the best and why.
Short version:
NOW() = CONCAT(CURDATE(), ' ', CURTIME());
CURDATE() = DATE(NOW());
Some explanation:
NOW() gets both date (CURDATE()) and time (CURTIME()).
So if we do it the other way round, CURDATE() = DATE(NOW()).
Regarding timestamp, in MySQL Data Types we can see timestampis 3 bytes, while datetime is 8 bytes.
I'm having trouble with MySQL format_date, and don't understand why. I have the following as part of my code:
date_format(NOW() + INTERVAL 3 DAY, '%Y-%m-%d')
which seems to work fine, except for the fact that regardless of the date I choose, the %d is returning as a single zero ('0'). If I change %d to %e I can get the correct date, but I'm using this to compare dates, so I need the leading zero for numbers below 10. Is this a database setting, or am I missing something obvious?
Thanks in advance.
UPDATE: I feel like it has to be something in the db, because when I simplify the query to this:
$q = "SELECT date_format(NOW() + INTERVAL 3 DAY, '%Y-%m-%d') as 'today' from content_field_date LIMIT 1";
'today' prints out as '2012-03-0'
Meanwhile, this:
$q = "SELECT date_format(NOW() + INTERVAL 3 DAY, '%Y-%m-%e') as 'today' from content_field_date LIMIT 1";
correctly returns '2012-03-17'
Zeth
I ran into this same issue today, based on your table name of "content_field_date" I'm assuming Drupal was being used. The answer to this can be found here:
Some sql queries work in terminal but not with db_query() function in Drupal 6 Why..?
and here:
http://drupal.org/node/100846
in that the percent signs need to be escaped, so the correct query should be:
$q = "SELECT date_format(NOW() + INTERVAL 3 DAY, '%%Y-%%m-%%d') as 'today' from content_field_date LIMIT 1";
WHat you describe should not happen. You either found a MySQL bug or you are doing something wrong.
If you only want to compare dates, you can do that inside MySQL and you probably shouldn't be using DATE_FORMAT() at all. You can use this to get a date:
DATE(NOW() + INTERVAL 3 DAY)
or:
(CURDATE() + INTERVAL 3 DAY)