简体   繁体   中英

Carbon - Comparing two dates by their days

Recently I have tried to compare two dates by their days. eg I want to check if the students have paid his/her fee in 5 of each month or not.

For more example, the student is registered in 2019-01-13 and now it is 2019-03-20 So I want to system notify me that the student fee time is passed 7 days. As well it should automatically check for next months.

You can define Task Scheduling

$schedule->command('student:fees_notify')->monthlyOn(5, '01:00');

You can schudele command based on your logic. it will triggered on 5th date on month start (01:00)

Your command will look something like

class StudentFeesNotify extends Command
{
   protected $signature = 'student:fees_notify';
   protected $description = 'your desc.....';

   public function handle()
   {
      // you can write your logic here
   }
}

It should look like this

use Carbon\Carbon // don't forget to import carbon in your class

$days = 5;

// i would recommend to use chunk if you have a lot of records & limit this query t only people that might be subject to this alert
$students= Student::get();
$notifyList = [];

// assuming that the registration date column is created at and it's a valid date

$students->each(function($student) use (&$notifyList,$days){
  $notify = Carbon::parse($student->registration_date)->addDays($days)->lt(Carbon::now());
  if($notify){
   $notifyList[] = $student;
  }
});

The array will contain the users that surpassed their date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM