Make laravel schedule command last day of month at time

Viewed 3967

I'm trying to run a command the last day of the month at 06:00, I tried doing this

$schedule->command('crawl')->when(function () {
return \Carbon\Carbon::now()->endOfMonth()->isToday();
})->at('06:00');

It does run on the last day, but the "at" does not work. Any idea how I can get it to work?

3 Answers

I'd simply use the monthlyOn() schedule method, because it gives you the opportunity to point at a given day of the month and a daytime to execute the command. Using php's date() function to retrieve the count of days in a month, this becomes an enjoyable task:

->monthlyOn(date('t'), '6:00');

You can run this; dailyAt instead of at function

$schedule->command('crawl')->dailyAt('06:00')->when(function () {
    return \Carbon\Carbon::parse('last day of last month')->isToday();
});

If you use Laravel version 7+ then you can use lastDayOfMonth inbuild method

$schedule->command('crawl')->lastDayOfMonth('06:00');
Related