Wahsei
Wahsei

Reputation: 307

Laravel Schedule Task - weekdays, every 2 hours , from specific to specify time

I have scheduled task which working is fine. every 2 hours , from 7:59, to 18:01.

 $schedule->call('App\Http\Controllers\Controller@function')->everyTwoHours()->between('7:59', '18:01'); 

However , I only want it to run from Monday to Friday so I added weekdays()

$schedule->call('App\Http\Controllers\Controller@function')->weekdays()->everyTwoHours()->between('7:59', '18:01');  

But unfortunately if will start from 00:00 every weekdays. This is not what I want. I want it to start from 8 am to 6pm every 2 hours.

+---------+---------------+-------------+----------------------------+
| Command | Interval      | Description | Next Due                   |
+---------+---------------+-------------+----------------------------+
|         | 0 */2 * * 1-5 |             | 2021-10-04 00:00:00 +08:00 |
|         | 0 */2 * * 1-5 |             | 2021-10-04 00:00:00 +08:00 |
+---------+---------------+-------------+----------------------------+

Appreciate your help, Thanks

Upvotes: 0

Views: 1451

Answers (1)

wanjaswilly
wanjaswilly

Reputation: 325

In your controller add this if check:

if(date('D') == 'Sat' || date('D') == 'Sun') { 
  //does nothing
} else {
  //your task
}

Upvotes: 1

Related Questions