Imagine we're giving users the ability to send emails using our website, but we want to limit them to not send more than 30 emails per day(24 hours).
So, by sending each email we're gonna insert a record into our table, then while he/she wants to send another one, we check if he has sent more than 30 emails during 24 hours or not.
How we could check this with PHP?
we query db, we got 20 records for this user, the date of records are:
2012-08-14 13:10:58
2012-08-14 12:45:47
2012-08-14 16:32:18
2012-08-14 19:10:40
...
...
...
How we could achieve such rule?
Thanks
Don't check it in PHP, use a simple query like this to get the answer out of he database right off the bat.
select
count(*)
from
yourTableName
where
dateCol>date_sub(now(), interval 1 day)
and userID=...
This will give you the count.
Edit: As Boris points out, this will count per day, you could change it to check for the last 24 hour period like this:
select
count(*)
from
yourTableName
where
dateCol>date_sub(now(), interval 24 hour)
and userID=...
Edit 2: After checking this, Riad correcly points out, these two do in fact return the same value. The 1 day is treated as exactly 1 day, not a calendar date. If the date column has a datetime of '2012-08-13: 13:00:00' a date_sub( dateCol, interval 1 day) will return '2012-08-12: 13:00:00'.
Related
I am creating a booking management system. I got stuck in the booking cancellation part. I want to allow users to cancel their orders if their booking time and the current time duration is between 2 hours because I want to restrict the users to cancel their booking if their booking time and current time duration is greater than or equal to 2 hours.
I want to generate a query that returns all the bookings whose booking time is less than 2 hours. How can I achieve this?
This is my database structure.
SELECT * FROM `TableName` where TIMEDIFF(NOW(),Your_date_ColumnName) < '02:00:00.000000'
Assuming that booking_time is in MySQL standard format.
Try this and the below query will use index if you have one in booking_time column
SELECT *
FROM booking_table
WHERE booking_time BETWEEN CURRENT_TIMESTAMP() - INTERVAL 2 HOUR AND CURRENT_TIMESTAMP()
You can extract the hour part of your date. Refer to this link
Then using this query to get those less than 2 hours.
SELECT * FROM table1 WHERE tdate - 120 < EXTRACT(MINUTE FROM now())
I am trying to fetch users that were inactive for N days from created date. I have dates stored in timestamp. I tried to pull it like that
SELECT name, created, accessed
FROM users
WHERE accessed >= DATE(NOW() - INTERVAL 3 DAY)
but it's not really working for me. I can't figure out how to build query to get inactive users that weren't active for N days from created.
Show users where last access was longer then 3 days ago:
SELECT
name,
created,
accessed
FROM users
WHERE ABS(TIMESTAMPDIFF(DAY, NOW(), accessed)) > 3
You can use DATE_ADD() to get inactive users by adding the N days to created column and then matching with accessed column. The example may be as:
SELECT name, created, accessed
FROM users
WHERE accessed >= DATE_ADD(created, INTERVAL 3 DAY)
DATE_SUB() uses to add time,days,years etc in specified date formated in timestamp.
For further study on this function please see here
https://www.w3schools.com/sql/func_date_add.asp
I Guess you can do here is
SELECT * from users where DATEDIFF(CURDATE(),accessed) > 3
CURDATE() - Current Date
accessed - last time account was accessed
3 - Difference of days.
Hope it Helps :)
Try changing NOW() with the column created. That way you are looking for users where accessed is more than the created date plus 3 days.
SELECT
name,
created,
accessed
FROM users
WHERE accessed >= DATE(created + INTERVAL 3 DAY)
When the authentication code in the Tuser is not null it means the users didnt login the site within 24 hours so i have to delete the rows.
Here is the query which I wrote but it is not deleting correctly
DELETE FROM tusers WHERE auth_code IS NOT NULL
AND auth_code !=''
AND STR_TO_DATE(NOW(),'%Y-%m-%d %T')
> (DATE_SUB(STR_TO_DATE(created_date,'%Y-%m-%d %T'),INTERVAL -1 DAY))
You are in a better position to determine whether it is right or not by testing and letting us know if it works or not.
But I can tell you that it is more complicated than it needs to be. You can compare dates directly like this:
... where date(now()) > date_sub(created_date, interval 1 day)
Note that if you are going to SUBTRACT a day in order to go back, you should subtract a positive number of days. Subtracting a negative number of days refers to later in time, not earlier.
I got the perfect delete which is working perfectly:
DELETE FROM tusers WHERE auth_code IS NOT NULL AND auth_code !=''
AND DATE_SUB(NOW() , INTERVAL 24 HOUR) > STR_TO_DATE(created_date,'%Y-%m-%d %T');
This query deletes the rows by comparing the current date of the system with the created date of the rows which reaches above 24 hours of the current date.
I have a table called reservations which has columns start_date and end_date.
If date_diff(start_date and end_date) is 30 then I need to send an email on 8,2 and 1 days before end_date. same with the below..
60 – 15,4 & 1
120 and above – 30, 8, 2 & 1...
any idea how to do this ??
set up cronjob to do this kind of stuff.
write 3 separate sql statements for your query get these to work.
then put these into a mysql_query in php setting a variable if row count >0
using isset on the variable into email smtp script
set up a cron job to check every morning.
SELECT *,DATEDIFF(CURDATE(), end_date) as DIFF FROM TABLE WHERE DATEDIFF(CURDATE(), end_date) in (1,2,8)
What you can do is you create 2 tables, 1 for the main reservation information and the 2nd one will be for the storing of the send dates (or send reminder dates).
When the saving the record you will then retrieve the difference between the current date and the end_date; if difference will be 30 then you'll have to store send_dates 8,2 and 1 days before the end_date; same with the other conditions you have.
Then create a cron job (that will run daily) to check the 2nd table of the send_dates = current date. Then email if conditions is met.
Hope this is the one you needed
I have a date and time column in my mysql table called start_date and start_time respectively. I want my users the ability to set reminders for themselves to be sent X hours before start_time, min. 1 hour and max 24 hours.
I'll be running a CRON script to send these reminders. Right now I do:
SELECT * FROM auctions WHERE start_date=CURDATE() AND status='0'
To get all the auctions that will be starting today and haven't yet started. My question is, how can I figure out if the time now is X hours before start_time so I can send them a reminder if it is.
Any suggestions at all?
Something like this:
SELECT col1, col2, col3
FROM records
WHERE (records.startDate BETWEEN NOW() AND ADDDATE(NOW(), INTERVAL 9 HOUR))
AND (records.status = '0');
Is there some reason why you can't just use a simple timestamp field instead of one for date and one for time. That way you could find all the ones that start in the next 5 hours (say), by doing
select * from auctions where start_ts between now() and now() + interval '5 hours';
Note: the interval syntax varies slightly between databases, and that's the one for postgresql, so you might have to change it slightly for mysql.
I actually did it this way before all the answers were sent and its working. Because i'm on a deadline I can't go back and change it :)
$sql="SELECT HOUR(ADDTIME(CURTIME(),'$hour')) as remindHour, HOUR(CURTIME()) as curHour";
$result=$this->db->query($sql);
extract($result->getAllSingle());
if ($remindHour <=$curHour) {
// Send reminders
}
Can you use unixtime to save the time?
Since PHP has a wonderful function called strtotime.
Within in you can say. strtotime("+20 hours") and get the unixtime for 20 hours from now.
Then its just a matter of which field is larger than the other, if so, send the notification.