Take the 2-minute tour ×

I need to delay the execution of some code in PHP (for example; sending an email) by 10 minutes after an event (form submission).

What is the best way to accomplish this?

Would be my only option be to run a Cronjob every minute? Is this practical on shared hosting?

share|improve this question
cron will be better there is no point of hang the script for 10 mins –  Tarun Sep 15 '12 at 5:41
Okay can you confirm some things here , Do you want a delay with in the cronJob , I mean , code in the cronjob has to be delayed ? –  aravind.udayashankara Sep 15 '12 at 5:41
A cron job would certainly be a reasonable way of doing it. It'd be practical if your hosting provides cron! Do you need the delay to be exactly n mins? That'd be slightly trickier with cron. If it's just at least n mins you'd be fine. –  therefromhere Sep 15 '12 at 5:42
It seems like your only options are a LOOONG-running script (which your hosting probably won't allow) or queuing tasks and executing them periodically with cron. –  Andrew Sep 15 '12 at 5:43
Caling the Quartz library from PHP might be an option. I don't know much about it (besides the fact that it is a Java library), if it is truly available to call in PHP, or if it uses cron under the covers. –  Merlyn Morgan-Graham Sep 15 '12 at 5:45
show 1 more comment

1 Answer

up vote 8 down vote accepted

Using cronjobs is the best way.

If you can't use a cronjob on your shared hosting (ask the customer support), you can run a cronjob on a machine connected to the internet (i.e. your home computer) that runs a wget to a php page on your server, authenticate on it and then run the php code to send your email.

For the PHP code part I'll use a database table with all the emails to be sent, a creation_date field and a status field.

Your PHP code called by the job will simply do (in pseudo code):

$batchRecords = takeAbunchOfRecordsWhereStatus(NOT_SENT);
while($batchRecords) {
    if($creationDate + 10 minutes >= now()) {
share|improve this answer
You beat me to it! –  Andrew Sep 15 '12 at 5:46
Running the cronjob on another computer is a cool idea, thanks! –  smilledge Sep 15 '12 at 5:50
Search for "web cron job" and you'll find lots of sites offering remote 'cron' services. –  txyoji Mar 4 at 20:44
add comment

Your Answer


