Jerry
Jerry

Reputation: 470

Run php script x hours after interaction (cronjob)

In my php script, I want a function to be executed 2 hours after an user has registered, whether he is logged in or not. I know it has somehow to be done with cronjob, but I have never worked with it before and I don't know how to pack this all together.

How would I go about this? I have looked around for answers but I haven't found a solution. My first idea was to store timestamps in a database and collect them with a query. A cronjob would then have to be run every few minutes to keep things updated. Would that be the right way? I would also appreciate some useful links for getting into cronjobs easily. Thanks in advance!

Upvotes: 0

Views: 247

Answers (1)

Kevin
Kevin

Reputation: 162

Yes that is how I would do it.

From command line:

crontab -e

Then enter the following on a new line:

* * * * * php /path/to/your/cron/script.php

This will run script.php every minute

You cron script would be something like follows:

$qry = "SELECT * FROM users WHERE signup_date < '$two_hours_after_now' AND cron_run = '0'"
// stuff you want to do
$qry = "UPDATE users SET cron_run = '1' WHERE id = '$id'";

Hopefully this will point you in the right direction.

Upvotes: 1

Related Questions