Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment
@simonhamp

simonhamp Feb 22, 2018

Thanks for sharing!

simonhamp commented Feb 22, 2018

Thanks for sharing!

@karlmonson

This comment has been minimized.

Show comment
Hide comment
@karlmonson

karlmonson Feb 27, 2018

Thank you for this trait!

karlmonson commented Feb 27, 2018

Thank you for this trait!

@adamlevenson

This comment has been minimized.

Show comment
Hide comment
@adamlevenson

adamlevenson Aug 6, 2018

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

adamlevenson commented Aug 6, 2018

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

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