I’m working on an application that sends alerts to users via email. The user chooses a time-frame and sets up any number of alerts to receive in that time-frame.
The goal is to send the alerts at equal time intervals.
For example a user sets up 8 alerts for 12 hours (this repeats daily). I use simple division to determine at which time intervals to send the 8 alerts one-by-one. 12 hours/8 alerts = intervals of 1 hour and 30 minutes. The code should be executed every 1h30m for 8 times.
What PHP functions can I use to create this algorithm? Please advise.
I think you would be better served using
cronor something similar. PHP, by default, will stop running any script after 30 seconds. If you’re using a hosted web provider, this is often non-negotiable.I will concede that some people write “console”-type applications that are run locally (e.g. via the command line), but since you didn’t say you’re doing this, I think we’re safe to assume that is not the case.
One way of using
cronwould be:Have the user specify the time interval and store it in a database. Then at some other interval (perhaps every minute, or every 5 minutes) check the database to see whether the last time you sent an email is longer than the requested interval. If it is, you would send another email and update the “last time sent” value in the database.
But having a PHP script “sleep”, or even worse, get locked in an infinite while loop, is not a good use of the system’s resources.