Last active
September 7, 2023 15:22
-
-
Save davidpiesse/be9db81995b45238a9008c1dcc4c25fd to your computer and use it in GitHub Desktop.
Laravel Custom Class/Model Scheduling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); | |
} | |
} |
Thank you David, you are the best!
Thank you. Excellent solution!
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
Thanks for this! Exactly what I needed, gave me the right path forward.