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
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.
How to write a sql query to find out that there are 2 days left before the current date.
In php, this can be done via:
$res['end_date'] - time () < 86400 * 3;
How can I do the same after 1 sql query, well or better, only 2 days, if less so that it does not work out, well, if it works out, it's okay.
UPD:
It is necessary to compose a sql query that will select only those records that have 2 days left before the end_date expires
The type is int for the field end_date and is stored via the time () function in php.
Can't compose a WHERE clause.
You can use the FROM_UNIXTIME function to convert it to a DateTime you can then use the NOW() plus 2 days to check if the date is under 2 days. You then have to check that the date is before the current time otherwise you'll get dates that have already gone.
SELECT
end_date
FROM
table
WHERE
FROM_UNIXTIME(end_date) <= NOW() + INTERVAL 2 DAY
AND
FROM_UNIXTIME(end_date) > NOW()
Assuming that you are storing an epoch timestamp (the number of seconds since January 1st, 1970), I would recommend:
select *
from mytable
where end_date >= unix_timestamp() and end_date < unix_timestamp() + 2 * 24 * 60 * 60
unix_timestamp() gives you the current epoch. You can use simple math to add two days to that.
The upside of this approach is that this does direct filtering against the store value, so this can take advantagae of an index on end_date - as opposed to converting the timestamp to a date, which requires converting the whole column before the filtering can happen. So this is much more efficient.
You can ajust the inequalities as you prefer. I used a half-open interval (inclusive on the lower bound and exclusive on the upper bound), which is a widely used approach.
I ended up doing this:
$time = time();
$params = $db->query("SELECT * FROM `params` WHERE (`end_date` - {$time}) < 86400 * 3");
And it worked.
I always do
select *
from mytable
where FROM_UNIXTIME(end_date) < NOW() + INTERVAL 2 DAY
This will get results where two days in the future is ahead of the end date ie, anything that will end within 2 days (or has already ended as I didn't add a check for that)
Edit: I see you can't use where
If you cannot use where clause
select FROM_UNIXTIME(end_date) - INTERVAL 2 DAY as end_date
from mytable
And then check in php if the result is before or after. This will show all results however
I have two columns in my database named dtp_s and dtp_e. Both of these columns hold strtotime() formatted ints which I then use in my PHP application to calculate hours/minutes between time intervals.
I want to display the 5 most recent records in date order, which works fine when I use this:
SELECT id
FROM _records
ORDER BY dtp_s DESC
LIMIT 5
However, I now want to convert the dtp_s back to a DateTime format in my Query and only pull out the data for that week. The issue I have is the records are for a weekly quota, my idea of pulling 5 records out covers Monday-Fri (which is all that is needed and uploaded) however, the following Monday will show the previous weeks Tuesday, Wednesday, Thursday and Friday as well.
I tried to use date_sub for a one week interval but this seems to only work on DateTime datatype columns, not a Unix timestamp:
SELECT id
FROM _records
WHERE dtp_s > DATE_SUB(NOW(), INTERVAL 1 WEEK);
ORDER BY dtp_s DESC
LIMIT 5
How only select the data that is from the current week by converting my formatted DateTime back to DateTime format? I appreciate any help in advance.
An example of my dtp_s and dtp_e is: 1595570400 1595584800
You can convert the filter value to a unix timestamp with date function unixtimestamp(), like so:
where dtp_s > unix_timestamp(now() - interval 1 week)
Actually, you can directly use unix_timestamp() with no conversion:
where dtp_s > unix_timestamp() - 7 * 24 * 60 * 60
Although unix_timestamp() can be very useful, unix_timestamp(now()) is actually redundant. You can just do the whole calculation in the domain of unix timestamps.
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)
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.