I have a mysql field set as "TIME", and I would like to check against that time, using time as well.
I will have a cron run every 10 minutes, where my clients can set reminder messages to be sent out every day at any time of the day or so.
Using the following query does not give me the fields that are from the last 9 minutes:
SELECT `id` FROM `sms_reminders` WHERE (`option` = "weekdays" OR `option_days` LIKE "%4%") AND `time` < DATE_SUB(NOW(), INTERVAL 9 MINUTE)
However, this is giving me mixed results.
My time field is set at 09:50:00 (Time of running the query is 09:53:00).
Thanks.
make few changes in your code use > this instead of <
After altering the query, I flipped the < to > and it is now working.
Related
I am working on a project where one "lights a virtual candle" and I want to create a cron job that selects all records from the database that are expiring in the next five days, possibly calculated from the databases "created_date" field which type is "Type: TIMESTAMP > CURRENT_TIMESTAMP"
Process:
The candle duration = 30 days
Alert period = 5 days before the 30 days
This is what I have so far (I can do the rest, it is the query I am having problems with)
$query_rsQueryA = "SELECT * FROM $databaseName WHERE created_date + INTERVAL 5 DAY < CURRENT_TIMESTAMP;";
$rsQueryA = mysql_query($query_rsQueryA) or die(mysql_error());
$row_rsQueryA = mysql_fetch_assoc($rsQueryA);
$totalRows_rsQueryA = mysql_num_rows($rsQueryA);
Thanks in advance!
It appears what I have written above actually works, should have tried it first guys, sorry about that!
You can use the following
datediff(now(),date_add(created_date,INTERVAL 25 day)) > 0
So basically its adding 25 day to the created_date and then finding the difference with the current date and if its greater than 0 meaning start sending alert
Please I am making a reminder to start sending mails for events starting in 7 days or less.
That is send mails to attendees to events starting in 0 - 7 days only.
Where I need help is on this line:
...WHERE event_start_date > '$current_time' //currently sends to all attendees for all events starting in future
$current_time = time();
How can I change that to reflect what I need?
Kindly note date is stored in unix_timestamp.
Thank you
You could also do this in MySQL only, without determining the time in PHP first. I don't think it's faster, but it's a lot more readable.:
...WHERE FROM_UNIXTIME(event_start_date) <= DATE_ADD(CURDATE(), INTERVAL 1 WEEK)
However, this also returns events starting before today. If you don't want that, you could use:
...WHERE FROM_UNIXTIME(event_start_date) BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 WEEK)
(adapted from the MySQL docs)
Get the current unix timestamp.
$current_time = time();
As it is stored in seconds, you can add to it by adding 60 seconds * 60 minutes * 24 hours * 7 days to get the unix timestamp in a week.
$weekFromNow=$current_time+(60*60*24*7);
Now just find events where the event_start_date is less than or equal to it.
...WHERE event_start_date <= '$weekFromNow'
and event_start_date > '$current_time'
//sends to all attendees for all events starting in future
Edit: as $weekFromNow is one week into the future, the query needs to find all times less than or equal to that value, which is what the query does. If your event_start_date holds some value OTHER than the unix timestamp of the event starting time, let me know.
This is my SQL statement, however I want to rewrite it to simple PHP, as I've already pulled out the lastlogin in to a variable, so how do I do it?
lastlogin > DATE_SUB(NOW(), INTERVAL 15 minute)
Let me try to reexplain perhaps... I have a if statement, I have a time saved in my database in format (0000-00-00 00:00:00) In order to the if statement to be executed, The time in database + 15 minutes must have passed :) And I don't get it to work
you can't rewrite it in PHP as it's obviously used to filter your data and should be used as is.
if (strtotime($lastlogin) > (time()+15*60))
Assuming you converted lastlogin to a unix timestamp:
if ($lastlogin > (time()+15*60))
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
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.