开发者

Retrieve a list of records based on time intervals (PHP/MySQL)

开发者 https://www.devze.com 2023-01-28 12:37 出处:网络
In my database table I have a DATETIME field called \'start_datetime\'. The time portion is always stored based on 5 min intervals, with no seconds, for example: 15:25:00 or 12:10:00 or 17:30:00

In my database table I have a DATETIME field called 'start_datetime'.

The time portion is always stored based on 5 min intervals, with no seconds, for example: 15:25:00 or 12:10:00 or 17:30:00

Now I need to set up a CRON task which will send out an email exactly 2 days before 'start_datetime'.

There are a couple of issues here:

1) First of all I need to FIND all records with a 'start_datetime' that is exactly 2 days before NOW (i.e. the time of running the query)

2) It should ignore any records that are more than, or less than 2 days away. Considering the times are based on 5 min intervals, I can only assume the CRON task would have to run every 5 mins? This could be a problem because my hosting provider only allows a CRON to run once开发者_高级运维 every hour max.

So what is the best way to approach this?


Is it really that important that it be accurate down to the 5-minute interval, or would it be acceptable to send that email up to 55 minutes early?

If so you can run a query that selects all the records that will hit that 2-day mark within the next hour.

Query would look something like this: (quick n dirty, untested)

SELECT * FROM table 
    WHERE CURRENT_TIMESTAMP >= DATESUB(startDate, INTERVAL 49 HOUR)
    AND CURRENT_TIMESTAMP < DATESUB(startDate, INTERVAL 48 HOUR);


Since you can run the cron only once an hour then you're only option would be to send emails for the selected values WHERE start_datetime is BETWEEN 2 days and 2 days and 1 hour , or you could setup a web service witch would get called every 5 min from you're own pc with a cron but it's not recomended .


Will your provider allow unlimited script execution time? If so, you could create a script that queries the database every five minutes via the sleep function.

Function:

http://php.net/manual/en/function.sleep.php

Pseudo Code:

function query_db() {
  //query code
}

for($i = 1; $i <= 60; $i++) {
  query_db();
  sleep(300);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消