Skip to content

Instantly share code, notes, and snippets.

@kasparasg
Forked from davidpiesse/Schedulable.php
Created June 25, 2018 23:42
Show Gist options
  • Select an option

  • Save kasparasg/ac775991302341f80bb445241fd57da7 to your computer and use it in GitHub Desktop.

Select an option

Save kasparasg/ac775991302341f80bb445241fd57da7 to your computer and use it in GitHub Desktop.

Revisions

  1. @davidpiesse davidpiesse revised this gist Feb 27, 2018. No changes.
  2. @davidpiesse davidpiesse renamed this gist Feb 27, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion HasSchedule.php → Schedulable.php
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,13 @@
    <?php

    //Don't forget to change the namespace!
    namespace App\Traits;

    use Cron\CronExpression;
    use Illuminate\Support\Carbon;
    use Illuminate\Console\Scheduling\ManagesFrequencies;

    trait HasSchedule{
    trait Schedulable{

    use ManagesFrequencies;

  3. @davidpiesse davidpiesse created this gist Feb 21, 2018.
    35 changes: 35 additions & 0 deletions HasSchedule.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    <?php

    namespace App\Traits;

    use Cron\CronExpression;
    use Illuminate\Support\Carbon;
    use Illuminate\Console\Scheduling\ManagesFrequencies;

    trait HasSchedule{

    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());
    }

    }