MySQL date/time calculation - php

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.

Related

How to filter MySQL dates?

I'm trying to do a SELECT * FROM but only items that are less than 30 days old. Here is my select code:
SELECT * FROM `{$table_name33}` WHERE `type`='wpst-requiredinfo' ORDER BY `foreignkey` ASC;
However, my problem is that I can't figure out how to add WHERE AND last_updated is less than 30 days.
I'm not exactly sure how to write the query, but the date is showing up like this: 1428412603 in the table column, it doesn't look much like a date to me. I don't know where to start.
Try this where clause:
WHERE `type`='wpst-requiredinfo' and
last_updated >= date_sub(now(), interval 30 day)
EDIT:
Your date seems to be in Unix time format.
WHERE `type`='wpst-requiredinfo' and
last_updated >= unixtime_timestamp() - 30*24*60*60
Note: this puts all the functions on the current time. In particular, it does not use FROM_UNIXTIME(last_updated). This ensures that an index can be used for this part of the query. The best index would be on (type, last_updated).

check if it happens in 24 hours

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'.

How to make a reminder in mysql with date stored in unix_timestamp

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.

Query to select records from a database that were created from a certain time in the past until now

I'm relatively a newbie, would appreciate help :)
I am looking to find entry(ies) from a mysql table which were created some time between now and a certain timestamp in the past. This time in the past is stored in a variable (say $timeinthepast, a few hours ago or yesterday, whatever). And the column 'timecreated' in the table is the timestamp of the creation of entry.
Would the following work? If not, what would?
Thanks!
<?php
$query = mysql_query("SELECT * FROM table1
WHERE timecreated = DATE_ADD(NOW(), INTERVAL -$timeinthepast)");
?>
I am basing this on: Query to select records from a database that were created within the last 24 hours
As long as $timeinthepast is valid (1 MONTH, 2 HOUR, e.g.) in the sql you can try BETWEEN
$query = mysql_query("SELECT * FROM table1
WHERE timecreated BETWEEN
DATE_SUB(NOW(), INTERVAL $timeinthepast) AND NOW()");
I find that the between syntax is the most freindly to use. I have not tested this sorry :)
Select * from `table1` where `timecreated` between DATE_SUB(NOW(), INTERVAL 1 month) and NOW()
You will need to make your time in the past a whole unit of some description.

Getting an event from a database a week in advancee

I am currently developing a sports website where one of the pages with be forthcoming fixtures in which the user will be able to what team and where the team are playing their next match.
I have a database with the following fields...
ID
TEAM NUMBER
OPPOSITION
VENUE
DATE
MEET TIME
MATCH TYPE
So a row of data pulled from the DB and print_r'd may look like this
ID=>[1] TEAM NUMBER=>[1] OPPOSITION=>[YORKSHIRE] VENUE=>[HOME] DATE=>[2009/4/25] MEET TIME=>[13.00] MATCH TYPE=>[CUP]
My problem is i cannot work out how to show the next match dependent on what the current date is, so for example for now I want the site to show all the games that will happen over the weeken of the 25th April 2009 and then once that has gone the fixtures for the next weekend.
Hope this makes sense and some one give me an idea of how to tackle this.
select * from my_events where date between now() and date_add(now(), interval 7 day);
Should do it I think.
Instead of relying entirely on MySQL, you can also use PHP's strtotime() function:
$query = "select * from my_events where date between now() and ".
date("Y-m-d", strtotime("+1 week"));
For MySQL check out the Date and Time functions. You can use a combination of CURDATE() and ADDDATE() to achieve what you need.
Your description is very vage but try something like this:
SELECT all_fields_you_need
FROM table_name
WHERE `DATE` > CURDATE() AND `DATE` <= DATE_ADD(CURDATE(), INTERVAL 7 DAY)
ORDER BY `DATE` ASC
(not tested, just written as it came into my mind...)
Load it all into an array and display the data
you can get the system date (in Oracle using sysdate) and then add to it, so look for all records where DATE = sysdate + 7. You may have to play with this a little, formatting the date so that sysdate + 7 returns a date without the time, but that is basically what you need.
EDIT:
If you want the event between now and a week from now (if games are only on the weekend, then this will return next weekend's games) do
DATE > sysdate AND DATE <= sysdate + 7
To get the next match for team xxx
SELECT *
FROM myTable
WHERE TEAM NUMBER = xxx
AND DATE = ( SELECT MIN(DATE)
FROM myTable
WHERE TEAM NUMBER = xxx
AND DATE > NOW() )
I suspect this is what you really want, if matches only take place at weekends (which seems to be an assumption from your question).
Today + 7 days is not the same as next weekend unless today happens to be the same day of the week as the match.

Categories