Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a "Next Run Time" date from a cronjob-like syntax

Tags:

php

In an app I'm creating a user is given the possibility to schedule a repeating task. Simply the values for generating the interval schema are:

Minute: [0-59, 90 (each minute)]
Hour: [0-23, 90 (each hour)]
Day of month: [1-31, 90 (each day of month), 91 (last day of month)]
Month: [1-12, 90 (each month)]

So, for example I've got this format: 10 - 2 - 90 - 90 which translates to 2015-07-16 2:10. The method determining the next run time date is able to present me this date. But, I'm looking for the efficient way to check if the next run time date has already passed (easy part) but then generate the first next run time date again. In this case it would be 2015-07-17 2:10.

I've found out that our brain is easily up to the task but I'm unsure which logical steps to follow to determine this in the most efficient way (without writing out all posibilities that is).

Any suggestions?

like image 907
Ben Fransen Avatar asked Jul 16 '15 14:07

Ben Fransen


People also ask

What does 0 * * * * mean in crontab?

0 * * * * -this means the cron will run always when the minutes are 0 (so hourly) 0 1 * * * - this means the cron will run always at 1 o'clock. * 1 * * * - this means the cron will run each minute when the hour is 1.

What is * * * * * In cron job?

What does * mean in Cron? The asterisk * is used as a wildcard in Cron. * sets the execution of a task to any minute, hour, day, weekday, or month.

How do I schedule a script in crontab to run every 5 minutes?

basic 3. /usr/bin/vim. tiny 4. /bin/ed Choose 1-4 [1]: Make a new line at the bottom of this file and insert the following code. Of course, replace our example script with the command or script you wish to execute, but keep the */5 * * * * part as that is what tells cron to execute our job every 5 minutes.


2 Answers

It is working fine. I am using PHP Cron Parser. It has everything that we needed.

Or else, you could use Cron Expression easier and more efficient. Some reference answers are already here and here in Stack Overflow.

For Cron Expression, you could use:

$cron = Cron\CronExpression::factory('@daily');
$cron->isDue();
echo $cron->getNextRunDate()->format('Y-m-d H:i:s');//this give next run date.
//echo $cron->getPreviousRunDate()->format('Y-m-d H:i:s');//this give previous run date.

And this is exactly the same what you have and Cron Expression also have,

enter image description here

All what you need to do is, just use PHP Cron Expression. Hope this helps.

like image 75
alagu Avatar answered Oct 29 '22 15:10

alagu


This is nearly Cron syntax, so why creating something own which does exactly the same job?

It should be pretty easy to switch to a valid cron syntax and use a simple library for parsing, etc.

An example would be https://github.com/mtdowling/cron-expression where you could get the next run date for free:

$cron->getNextRunDate()->format('Y-m-d H:i:s');

You would also have the possibility to use values like 0/15 to run every 15 minutes, etc. Another benefit is, that cron syntax is common knowledge which prevents misinterpretation of the format.

like image 23
echox Avatar answered Oct 29 '22 15:10

echox