Rajan Rawal
Rajan Rawal

Reputation: 6317

PHP : Schedule a task at user specific date and time

I need to create a functionality like google calendar, where user have rights to change the date and time for reminder to get email or sms. I have googled and found that only solution is to use cron job.

However cron jobs runs at specific time repeatedly ( based on how we set it ). We can do it via cron job. But what i want in that case, user can change the time of reminder and then how to maintain it via cron job? Kind of user specific cron job

Does it mean dynamically creating a cron job? It is possible? If Yes, is there server permission issue? I have shared hosting server.

Upvotes: 0

Views: 1787

Answers (3)

Kiran Maniya
Kiran Maniya

Reputation: 8979

There is a PHP Library you can use to carry the task, install it via,

composer require hutnikau/job-scheduler

and here is the sample code,

$startTime = new \DateTime('2017-12-12 20:00:00');
 $rule = new \Scheduler\Job\RRule('FREQ=MONTHLY;COUNT=5', $startTime); //run monthly, at 
 20:00:00 starting from the 12th of December 2017, 5 times
 $job = new \Scheduler\Job\Job($rule, function () {
    //do something
 });

Setup library and required cron job by the job scheduler, otherwise it won't work.

As an alternative you can use Cron Job in your server, Cron Job simply executes the script defined in the job periodically. By using Cron Job you will be able to control how many times it should execute in the year, month, day, hour, minute and seconds.

Upvotes: 1

Oscar M.
Oscar M.

Reputation: 1086

The at command will let you schedule one off tasks to run, similar to cron. The "run a cron job every minute" will work until each one is doing so much work that execution time exceeds 60 seconds.

Honestly, this sounds like it should be handled via a message queue or workers.

Upvotes: 0

laurent
laurent

Reputation: 418

A solution may be to set the cron job every minute

* * * * * /path/to/php /var/www/html/a.php

and in your script you check all users having alerts current_hour:current_minute and send them alerts ...

Upvotes: 1

Related Questions