Date/time calculation

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm trying to come up with a function using PHP/MySQL (preferably a PHP only
solution) to find the next time that matches a particular pattern, where the
pattern is a combination of values or wildcards for minute, hour, day,
weekday, month (like a predictive version of Unix's cron.)

So, for example, I might ask for the next time that will match 8:00AM on a
Monday, the next time that will match 15 minutes past midnight on the 1st of
the month, or the next time it will be 11:59 on a Friday the 13th.

It is quite easy to figure out whether or not a particular time matches
these criteria, but I can see no way to predict when the next time it will

Any suggestions?

- Kevin

Re: Date/time calculation

Kevin Lin wrote:
Quoted text here. Click to load it

Check if today matches the criteria;
if it doesn't, check tomorrow;
if tomorrow doesn't match, check the next day;
.... ... ... ...

Yes, I know it's slow (next Thursday Christmas is due in 1520 days!)

$match = time();
while(true) {
  if (day_month_week_check($match)) break;
  /*  ^^^^^^^^^^^^^^^^^^^^ TODO */
  $match += 24*60*60;
/* now deal with hours and minutes :) */
USENET would be a better place if everybody read:    | to  mail  me:  simply |    | "reply" to this post, |      | *NO* MIME, plain text |                     | and *NO* attachments. |

Site Timeline