Wednesday, March 16, 2022

[SOLVED] Laravel Scheduler: how to run task on weekdays only?

Issue

I am writing Laravel scheduler task and want to run it once a day every weekday (From Monday to Friday).

I see that rel="nofollow">Task Scheduler has ->weekdays() option, that presumably does precisely what I want. But I wasn't able to find a confirmation or description of this option that says it will run from Monday to Friday and not, say, from Monday to Saturday.

Also I would like to run the task at specific time. I see there is a ->dailyAt('13:00'); method. I'd like to know the best solution to run task at weekdaysAt.

Thanks in advance!

P.S. I use Laravel 5.2, in case that matters.


Solution

I guess this is a confirmation, the source code of the weekdays() method:

public function weekdays()
{
    return $this->spliceIntoPosition(5, '1-5');
}

You can find it here: \vendor\laravel\framework\src\Illuminate\Console\Scheduling\Event.php

Right after weekdays() you will see mondays() method, which shows that Laravel counts mondays as "day 1":

public function mondays()
{
        return $this->days(1);
}


Answered By - Alexey Mezenin
Answer Checked By - Willingham (WPSolving Volunteer)