Get Records 30 minutes before date and time - php

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.

Related

How to select top-of-the values from database with minute resolution

I have a mysql(i) databse that is written to every minute (usually 4 or 5 seconds after the minute).
I would like to select the values that are cleset to top-of-the-hour for the last 36 hours and I've no idea how to do it.
I've been playing with INTERVAL and DATE_ADD but have not found something that works yet. Any help would be appreciated.
Edit:
Extra info:
Table name:
temperature
Column names:
uid (AI)
time (timestamp)
probe0
probe1
probe2
probe3
probe4
Perhaps it would also be better to be
now
now -1hour
now -2hours
etc
now -36hours
FWIW, I'm currently using the following code so select ALL the data for the last 36 hours (2160 rows)
SELECT time, probe0, probe1, probe2, probe3, probe4 FROM temperature WHERE temperature.time >= DATE_ADD(NOW(), INTERVAL -2160 MINUTE) ORDER BY temperature.time DESC
If you want to get the first record for each hour you can use MySQL's MIN() function and since it is an Aggregate Function you need to use GROUP BY to group your data by date and hour. So a sample query will look like:
SELECT
MIN(`time`) as first_for_hour, probe0, probe1, probe2, probe3, probe4
FROM `temperature`
WHERE `time` >= DATE_ADD(NOW(), INTERVAL -2160 MINUTE)
GROUP BY DATE(`time`), HOUR(`time`);
and for reading data 36 hours back I used your WHERE clause:
WHERE `time` >= DATE_ADD(NOW(), INTERVAL -2160 MINUTE)
I hope this will be helpful to you and here is the playground.

MySQLi CURDATE() today and 2 hours in to tomorrow?

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)

MySQL Filter for Vicidial

I work for a call center doing tech support, our Dialer uses filters that are written in MySQL. I am a complete novice and cannot wrap my head around it no matter how many tutorials I try.
The only example I can find in the manual is:
called count >= 7
This will only call leads with 7 or greater attempts.
The above syntax redacts the SELECT and WHERE statements because apparently the filter is merely a WHERE statement appended to the standard query.
We have a field called entry_date it is in the format of 11/17/2015 9:48:16 AM (as are all our date entries) I am attempting to filter by results greater than 30 days from the current date.
I tried
entry_date - NOW() >= 30
And it doesn't work :(
Any assistance would be most appreciated.
Ryan
Here is a real example of a VICIdial filter that is used to prevent NI(Not Interested) status leads from being dialed within 30 days of their last call:
( ( (status='NI') and (last_local_call_time < CONCAT(DATE_ADD(CURDATE(), INTERVAL -30 DAY),' ',CURTIME()) ) ) or (status != 'NI') )
You may have an issue with your date format if it is stored in the database as you indicate. The following will select within the last 30 days if entry_date is a datetime field.
entry_date > NOW() - INTERVAL 30 DAY
yes, well, the mysql date functions are a bit bizarre. Here is the way to compute the date 30 days in the past:
mysql> select DATE_SUB(CURDATE(), INTERVAL 30 day) as past;
+------------+
| past |
+------------+
| 2015-10-20 |
+------------+
and I guess you will need to convert the timestamp of your entry_date field to just a plain date without the time, something like this:
DATE(entry_date)
so then I would guess that to get all records older than 30 days would be:
DATE(entry_date) < DATE_SUB(CURDATE(), INTERVAL 30 day)
As suggested by #Sean, MySQL datetime format is yyyy-mm-dd hh:mm:ss
so you will first have to conver your datetime MySQL datetime using STR_TO_DATE function
STR_TO_DATE(entry_date, '%Y-%m-%d');
then using DATE_SUB function you can find last 30 days from current date
DATE_SUB(CURDATE(),INTERVAL 30 DAY)
So, your condition would be
STR_TO_DATE(entry_date, 'Y-m-d') < DATE_SUB(CURDATE(),INTERVAL 30 DAY)
Please see MySQL Date and Time Functions for further reference.
If you want to call a number that was not called in last 30 days then try.
last_local_call_time < DATE_SUB(NOW(), INTERVAL 30 DAY)

Mysql queries for date difference in days

Need help here, having an mysql table called APPROVAL, there having an id,dateandtime and level, i need a query that selects the id alone with the following condition.
Taking date alone from database and comparing it with current system date, if the days exceeds above 30 and below 60 and also level = 5.
How can I write a query for this.
Thanks in advance.
MySQL has good date arithmetic. For example, the expression
CURDATE() + INTERVAL 30 DAY
gives a datetime value denoting midnight 30 days hence. Similarly
CURDATE() + INTERVAL 61 DAY
yields midnight on the 61st day.
So a query of the form
SELECT ID
FROM APPROVAL
WHERE Level = 5
AND `DateTime` >= CURDATE() + INTERVAL 30 DAY
AND `DateTime` < CURDATE() + INTERVAL 61 DAY
will yield what you want. Notice the use of >= for the beginning of the range of days, and the use of < and an extra day for the end of the range. We do that because we want all items from the 60th day, and none from the 61st day.
A compound index on (Level, DateTime) will make this query very efficient to satisfy.
Notice that an expression like
DATE(`DateTime`) <= CURDATE() + INTERVAL 60 DAY /* slow! */
will also yield correct results, but the presence of the the DATE() function call on the column to be searched makes it unsargeable. That is, it makes MySQL unable to use an index to satisfy the search.
Ok so use this query to retrieve all the IDs that match level 5 and date diff between 30 and 60 compared to the current date.
SELECT id
FROM APPROVAL
WHERE level = 5 && DATEDIFF(NOW(), dateandtime) BETWEEN 30 AND 60
I'd suggest you to order them dy date DESC too.
Hope that helps
I hope, I understood your problem correctly.
select `ID`
from APPROVAL
where `Level` = 5
and ( DATE(`DateTime`) > curdate() + interval 30 day
and DATE(`DateTime`) < curdate() + interval 60 day )
order by `ID` asc;
Where DATE() gets the date from a datetime and CURDATE() is the current system date. With interval you can manipulate a date expression whitout having to worry about its limits.

adding 30 minutes to datetime php/mysql

I have a datetime field (endTime) in mysql. I use gmdate() to populate this endTime field.
The value stored is something like 2009-09-17 04:10:48. I want to add 30 minutes to this endtime and compare it with current time. ie. the user is allowed to do a certain task only 30 minutes within his endTime. After 30 minutes of his endTime, they should not be allowed to do a task.
How can this be done in php?
I'm using gmdate to make sure there are no zone differences.
If you are using MySQL you can do it like this:
SELECT '2008-12-31 23:59:59' + INTERVAL 30 MINUTE;
For a pure PHP solution use strtotime
strtotime('+ 30 minute',$yourdate);
Try this one
DATE_ADD(datefield, INTERVAL 30 MINUTE)
MySQL has a function called ADDTIME for adding two times together - so you can do the whole thing in MySQL (provided you're using >= MySQL 4.1.3).
Something like (untested):
SELECT * FROM my_table WHERE ADDTIME(endTime + '0:30:00') < CONVERT_TZ(NOW(), ##global.time_zone, 'GMT')
Dominc has the right idea, but put the calculation on the other side of the expression.
SELECT * FROM my_table WHERE endTime < DATE_SUB(CONVERT_TZ(NOW(), ##global.time_zone, 'GMT'), INTERVAL 30 MINUTE)
This has the advantage that you're doing the 30 minute calculation once instead of on every row. That also means MySQL can use the index on that column. Both of thse give you a speedup.
Use DATE_ADD function
DATE_ADD(datecolumn, INTERVAL 30 MINUTE);
Get date from MySQL table by adding 30 mins
SELECT loginDate, date_add(loginDate,interval 30 minute) as newLoginDate
FROM `tableName`;
This will result like below
Login Date - 2020-07-22 14:00:00
New Login Date - 2020-07-22 14:30:00

Categories