Skip to content

Instantly share code, notes, and snippets.

@davidpiesse
Last active September 7, 2023 15:22
Show Gist options
  • Save davidpiesse/be9db81995b45238a9008c1dcc4c25fd to your computer and use it in GitHub Desktop.
Save davidpiesse/be9db81995b45238a9008c1dcc4c25fd 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());
}
}
@simonhamp
Copy link

Thanks for sharing!

@karlmonson
Copy link

Thank you for this trait!

@adamlevenson
Copy link

Thanks for this! Exactly what I needed, gave me the right path forward.

@digitiqman
Copy link

Thank you David, you are the best!

@jakubgrzaslewicz
Copy link

Thank you. Excellent solution!

@r-walloner
Copy link

Thanks! Just what I needed for my current Project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment