I am retrieving results from my database using the current date.
My current code looks like this:
$sql = "SELECT *
FROM `1ymzj0g_orders`
WHERE `processed` = '1'
AND DATE(`order_date`) = CURDATE() LIMIT 100";
This is great for getting results from TODAY although for my circumstances, it needs to get the results from today and up to 2am tomorrow morning.
I am guessing under the current layout after midnight my results will clear, however I need to keep the results after midnight, but not for two whole days, just up to about 2am.
Is this possible? And if so could you provide me with some advice here?
you can use DATE_ADD with an HOUR interval to set a limit on the order_date
some thing similar to this :
WHERE DATE(`order_date`) = CURDATE() OR DATE(`order_date`) < DATE_ADD(CURDATE(), INTERVAL + 12 HOUR)
Related
Been trying to get this to work for 2 days and this is frustrating me.
Trying to get records 30 minutes before a date/time (Format in database is datetime).
This is what I have:
select id
from tbl_events
WHERE DATE_SUB(NOW(), INTERVAL -30 MINUTE) = DATE_FORMAT(start, '%Y-%m-%d %H:%i:%s')
What the heck am I missing?
Thanks
You already use the function DATE_SUB() so within that function you can simply use INTERVAL 30 MINUTE without the minus sign.
You also don't have to format start if it is a datetime or timestamp field.
Finally you shouldn't use = because times are hardly every exactly equal.
This gives this query:
select id
from tbl_events
WHERE start < DATE_SUB(NOW(), INTERVAL 30 MINUTE)
Probably. It's not extremely clear what you're trying to do.
I'm trying to display all attacks which have happened today. This is the current SQL I have in my php page. However it doesn't display how many have happened today.
$TodayAttacks = $odb->query("SELECT COUNT(id) FROM `logs` WHERE `date` BETWEEN DATE_SUB(CURDATE(), INTERVAL '-1' DAY) AND UNIX_TIMESTAMP()")->fetchColumn(0);
If by "today" you mean "since midnight today", your query should look something like this:
SELECT COUNT(id) FROM logs
WHERE date >= CURDATE();
If there is a possibility of date values in the log greater than today's date (hey, weird stuff happens!) then you want this (I advise against using BETWEEN for date comparisons):
SELECT COUNT(id) FROM logs
WHERE date >= CURDATE()
AND date < DATE_ADD(CURDATE(), INTERVAL 1 DAY);
If you mean "within the last 24 hours", your query would look more like this:
SELECT COUNT(id) FROM logs
WHERE date >= DATE_SUB(NOW(), INTERVAL 1 DAY);
Hope this helps.
EDITED per comments below
If the date column is a Unix timestamp value (stored as INT(11)?) then the query should look something like the following:
SELECT COUNT(id) FROM logs
WHERE date >= UNIX_TIMESTAMP(CURDATE());
One could also do the following to get records from the last 24 hours:
SELECT COUNT(id) FROM logs
WHERE date >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY));
SQL Fiddle here.
I have a MySQL table with the following columns:
id, month, day, remind_days_before
It's for a simple recurring reminder function. The user can enter for example: "I have a very important thing on the 5. April every year, please remind be 15 days before that". There could be tens of thousands of entries in this table. Using this table I want to run a cron_job every morning, which is sending out this reminders in form of email messages. The problem is I don't know how to write this kind of SQL query... Is it even possible? I want to query only those rows where the "month" and "day" as DATE is between TODAY and TODAY+31 days (this is the maximum number for remind_days_before).
I'm trying with this right now, but the $end_month is giving me the same as the $today_month:
$today_month = date('m');
$end_month = date('m', strtotime('+31 days', $today_month));
What happens when the +31 days DATE is in the next YEAR or we have leap year?!
Can someone help me out here?
Thank you very much.
First of all, your fields must be a no-mysql function.
So replace month/day with planning_month/planning_day, because the month and day words, are the functions of MySQL, and you may have errors if they are not quoted
Here's a working query. I've tested and works great:
select * from MyTable
/*CHECK CURRENT YEAR*/
where date(concat(year(now()), planning_month, planning_day)) =
date(date_add(now(), interval remind_days_before DAY))
OR
/*CHECK NEXT YEAR*/
date(concat(year(date_add(now(), INTERVAL 1 YEAR)), planning_month, planning_day)) =
date(date_add(now(), interval remind_days_before DAY))
Live Example
Your, it is a very useful question for me too
Just check for both years:
select *
from your_table
cross join (select year(now()) as current_year, year(now())+1 as next_year) vars
where curdate() + interval remind_days_before day = concat(current_year,'-',month,'-',day)
or curdate() + interval remind_days_before day = concat( next_year,'-',month,'-',day)
I'm working on a invoice system for a application of mine. I want to invoice the users every 2 weeks. There is a cronjob every week with the check if the user gets an invoice. But it give me some bugs, because it has been a new year and the system gives someone a invoice even when they have got them a week ago.
This is my query:
SELECT *
FROM user
WHERE DAY(registered) = DAY(DATE_SUB(CURDATE(), INTERVAL 2 WEEK))
OR DAY(registered) = DAY(NOW()) AND registered != CURRENT_DATE()
Thank you!
You can try this- (code is tested) Demo
SELECT * FROM user WHERE registered > CURRENT_DATE() AND
MOD((FLOOR( DATEDIFF( now( ) , `registered` ))),14) = 0
The day function in MySQL returns the day of month. You will clearly have problems with your query when the time period spans month boundaries.
I think you want logic of this form:
SELECT *
FROM user
WHERE registered >= DATE_SUB(CURDATE(), INTERVAL 2 WEEK)) and
registered < curdate()
(Or <= if you want the current date to be invoiced.)
I do not know what this is doing:
OR DAY(registered) = DAY(NOW()) AND registered != CURRENT_DATE()
If the day of the month is the same for registered and now(), then registered should be the current date. The only difference between now() and curdate() is that the former includes the time stamp. I would also say that it is bad practice to mix three different ways of getting the same date in the same query, even if they are equivalent.
I'm completely stumped on this one, being trying for hours but with no success, hoping someone can help. Trying to build a cron script to run daily that returns the rows that are exactly 7 days older than the current date.
The thing is, my query is not returning anything. No error messges, nothing (I know there are entries in the DB from the last 7 days - we get about 7000 new entries a day, so they are there!) I've tried a SELECT * and echo out the edit date with success, so everything is working, apart from my SQL script.
The column I'm referencing (edit_date) is type 'datetime' formated with Y-m-d h-m-s. This column always has a datetime value assigned on both create and edit.
function get_ad_sql($table){
$sql = "SELECT
*
FROM
".$table."
WHERE
edit_date = DATE_SUB(NOW(), INTERVAL 7 DAY)
";
return $sql;
}
And calling the function and 'trying' to echo the primary_key:
$sqlAng = get_ad_sql('angebote');
$result = mysql_query($sqlAng);
while($row = mysql_fetch_array($result)){
echo $row['primary_key'];
}
I've tried every variation of DATE_SUB(NOW(), INTERVAL 7 DAY), including CURDATE(), DATE_FORMAT(edit_date, '%m/%d/%Y') that I could find on here and online, but couldn't get anything to work. Hope someone can help me!
It is very rare to get same datetime entries which gives date and time upto seconds. Therefore, for getting appropriate results we need to ignore the time part, and deal with date part, thus, using CURDATE() function.
You could do that ignoring the time part and compare with the date using following:
function get_ad_sql($table){
$sql = "SELECT
*
FROM
".$table."
WHERE
DATE(edit_date) = DATE_SUB(CURDATE(), INTERVAL 7 DAY)
";
return $sql;
}
NOW() returns DATETIME value, you should use a DATE function to get date without time, e.g. -
SELECT * FROM table WHERE edit_date = DATE_SUB(DATE(NOW()), INTERVAL 7 DAY);
If type of edit_date field is DATETIME, then this field should be wrapped by DATE() function too -
SELECT * FROM table WHERE DATE(edit_date) = DATE_SUB(DATE(NOW()), INTERVAL 7 DAY);
Your script is working... I highly doubt you have something exactly 7 days ago (to the second).
Perhaps you wanted something WHERE edit_date>DATE_SUB(NOW, INTERVAL 7 DAY) AND edit_date<DATE_SUB(NOW, INTERVAL 6 DAY)?
Or, if you want to compare just the date (not the time) portions, compare the output of DATE() instead.
SELECT SUBDATE(CURDATE(), 7)
Try this.