Closed Oguntoye closed 6 years ago
@Oguntoye by passing the date to the run
method you're effectively faking the current time in the system. The job has no specified time to run so it's assumed it should run every minute.
To give you an example, the above code is registering a new job to run every minute, then when the scheduler gets executed it's assuming the current date is 2018-09-04 18:48:00
instead of the actual now
date, so every time the scheduler runs it will always run as it was that date/time.
Passing the date to the run
method is primarily used to facilitate testing of the scheduler (you can find more details here https://github.com/peppeocchi/php-cron-scheduler/pull/28#issue-140815888)
Instead, if you want to run your script at a specific date and time, you might do so by using a cron expression, so your code will look like
$scheduler = new Scheduler();
$scheduler->php($pathtophpscripttorun, '/usr/local/bin/php')->at('48 18 4 9 *');
$scheduler->run();
Just a note, that will execute the script every year on that date and time (cron don't have support for the year), another workaround would be to add a condition before scheduling the job. Example of pseudocode below
$scheduler = new Scheduler();
$datetorun = new DateTime('2018-09-04 18:48:00');
if ($datetorun === now) {
$scheduler->php($pathtophpscripttorun, '/usr/local/bin/php');
}
$scheduler->run();
This would be a great feature to build into the scheduler. I'll add to the list for the next release.
I couldn't get a script run at a specified date.. This is my code... it is not executing at the date specified it is rather executing instantly.. please help me.
$datetorun = new DateTime('2018-09-04 18:48:00'); $scheduler = new Scheduler(); $scheduler->php($pathtophpscripttorun, '/usr/local/bin/php')->run($datetorun);
what am i doing run.. please help.. i want the skip to run at the date specified.