简体   繁体   English

Carbon-按日期比较两个日期

[英]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. 例如,我想检查一下学生是否在每个月的5日内支付了学费。

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. 例如,学生在2019-01-13注册,现在是2019-03-20因此我想系统地通知我,学生费用时间已过去7天。 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) 它将在月份开始的第5个日期(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 该数组将包含超过其日期的用户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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