Ankit
Ankit

Reputation: 1114

How to run laravel cron after every 2 weeks or 14 days

I need to run laravel cron for after every 2 weeks or 14 days. but not found any solution for this. I was also search on laravel documentation https://laravel.com/docs/5.5/scheduling and found this option

->weekly();

But it runs on weekly. I was search also other option and found this laravel schedule task to run every 10 days of a month But it work only first 10 days of a month

$schedule->command('log:test')->cron('0 0 1-10 * *');

Please help me if you have any solution thanks in advance.

Upvotes: 7

Views: 6066

Answers (3)

Alican Ali
Alican Ali

Reputation: 181

$schedule->command('command_name')->weekly()->mondays()
         ->when(function () {
             return date('W') % 2;
          })->at("13:38");

Function will return 1 in every two other weeks, So the command will be called in every two weeks.

Upvotes: 6

Harsha
Harsha

Reputation: 176

You can use daily() together with when() method and inside when add suitable constrain for your task. For example, run task every month on 1,16 days:

$schedule->command('sitemap:sitemap_xml_generate')->daily()->when(function () {
$days = [1,16];
$today = Carbon::today();
return in_array($today->day, $days);});

or you can use this

$schedule->command('sitemap:sitemap_xml_generate')->cron('0 0 1,16 * *');

cron('0 0 1,16 * *') -> Argument are 'minute hour day(we can specify multiple date seperated by comma) month year'

Upvotes: 4

the_hasanov
the_hasanov

Reputation: 843

$schedule->command('log:test')->cron('0 0 */14 * *');

2017-09-15 00:00:00

2017-09-29 00:00:00

2017-10-01 00:00:00

2017-10-15 00:00:00

2017-10-29 00:00:00

or you can run your custom

    $schedule->command('log:test')->weekly()->when(function () {
        return ((Carbon::now()->day % 2) === 1); //bool only for example
    });

Upvotes: 0

Related Questions