Parth Vora
Parth Vora

Reputation: 4114

Run cron on last monday of the month - Laravel

I want to run a cron only on last Monday of the month using Laravel's Cron API. I have checked this:

https://laravel.com/docs/5.3/scheduling

I don't get any solution there.

At most I can:

$schedule->command(Commands\PerformerReminder::class)->monthly()->mondays();

But I think it will run the cron on each Monday of the month, not just on the last Monday.

If that is not possible using Laravel's cron API, them I'm fine with using regular cron options.

I think that will be like this:

$schedule->command(Commands\PerformerReminder::class)->cron('* * * * *);

I don't know what will be the value inside cron() function.

Any help would be appriciated.

Thanks,

Parth Vora

Upvotes: 0

Views: 1179

Answers (2)

Parth Vora
Parth Vora

Reputation: 4114

I got that working with this:

App/Console/Kernal.php:

protected function schedule(Schedule $schedule)
{
    $schedule
        ->command(Commands\PerformerReminder::class)
        ->when(fn() => Carbon::parse('last monday of this month')->isToday());
}

Upvotes: 2

Cerlin
Cerlin

Reputation: 6722

How about

use Carbon;

$schedule->command(Commands\PerformerReminder::class)->mondays()->when(function(){
    // return true if this monday is last monday of this month

    return (Carbon::now()->isSameDay($now->lastOfMonth(Carbon::MONDAY)));
});

Upvotes: 2

Related Questions