Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

flashman's avatar

Schedule command on specific date

Hi,

I want to schedule command on specific date, that date is stored in database. How to inject that date into Laravel shedule command?

Also, is it possible to schedule command to run:

  • two days before selected date
  • one day before selected date
  • on day when date is that day at specific time, e.g. 12:00

For example: date in database is: 20.06.2016 I want command to run on: 18.06.2016 19.06.2016 20.06.2016 as 12:00

Thanks

0 likes
3 replies
bobbybouwmann's avatar

There is no way to do that at the moment as far as I know.

What you can do is run one command every day or every hour and check if one of the items in the database matches that date and time and then run the command?

1 like
martinbean's avatar

@lukacavic You could use the truth test to determine if a command should be fired or not.

$schedule->command('your:command')->daily()->at('12:00')->when(function () use ($dateInDatabase) {
    return (
        $dateInDatabase == Carbon::today() ||
        $dateInDatabase == Carbon::yesterday() ||
        $dateInDatabase == Carbon::subDays(2)
    );
});

This will trigger every day at 12:00, but will only run the command if the date in your database matches the current date, yesterday, or two days ago.

11 likes

Please or to participate in this conversation.