Search code examples
laravelcronscheduled-taskscron-task

Task Scheduling on custom day Laravel


I want do this job oly on this days can I use at() with when() ????

$schedule->command('mycommand')->when(function () {
            $day = Carbon::today()->day;
            if($day == 3 || $day == 5 || $day == 7 || $day == 22) {
                return true;
            } else {
                return false;
            }
        })->at('17:00');


Solution

  • You can use this way :

    $schedule->command('mycommand')->monthlyOn(3, '17:00');
    $schedule->command('mycommand')->monthlyOn(5, '17:00');
    $schedule->command('mycommand')->monthlyOn(7, '17:00');
    $schedule->command('mycommand')->monthlyOn(22, '17:00');
    

    Or, simpler way, using cron :

    $schedule->command('mycommand')->cron('0 17 3,5,7,22 * *');