mtdowling / cron-expression

CRON for PHP: Calculate the next or previous run date and determine if a CRON expression is due
http://mtdowling.com/blog/2012/06/03/cron-expressions-in-php/
MIT License
4.89k stars 339 forks source link

Potential issue with getPreviousRunDate() every minute #46

Closed RhodriM closed 10 years ago

RhodriM commented 10 years ago

Hi,

When the following code:

$cron = Cron\CronExpression::factory('* * * * *');
echo $cron->getPreviousRunDate()->format('Y-m-d H:i:s');

is ran at 10:51:20 , the output is 2014-03-19 10:50:00.

Surely if the cron is scheduled to run every minute, the last run time should be 10:51:00?

Regards,

Rhodri

mtdowling commented 10 years ago

You need to allow the current time to be returned by the function. See: https://github.com/mtdowling/cron-expression/blob/master/src/Cron/CronExpression.php#L172

$cron = Cron\CronExpression::factory('* * * * *');
echo $cron->getPreviousRunDate('now', 0, true)->format('Y-m-d H:i:s');
RhodriM commented 10 years ago

Thanks; sorry - not sure how I missed that param!