Skip to content

Instantly share code, notes, and snippets.

@fraol19
Forked from davidpiesse/Schedulable.php
Created October 15, 2020 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fraol19/5b88634d6534c5ef2125529f717bd23b to your computer and use it in GitHub Desktop.
Save fraol19/5b88634d6534c5ef2125529f717bd23b to your computer and use it in GitHub Desktop.
Laravel Custom Class/Model Scheduling
<?php
//Don't forget to change the namespace!
namespace App\Traits;
use Cron\CronExpression;
use Illuminate\Support\Carbon;
use Illuminate\Console\Scheduling\ManagesFrequencies;
trait Schedulable{
use ManagesFrequencies;
protected $expression = '* * * * *';
protected $timezone;
public function isDue(){
$date = Carbon::now();
if ($this->timezone) {
$date->setTimezone($this->timezone);
}
return CronExpression::factory($this->expression)->isDue($date->toDateTimeString());
}
public function nextDue(){
return Carbon::instance(CronExpression::factory($this->expression)->getNextRunDate());
}
public function lastDue(){
return Carbon::instance(CronExpression::factory($this->expression)->getPreviousRunDate());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment