Carbon-按日期比较两个日期

时间:2019-03-18 13:21:41

标签: php laravel date php-carbon

最近,我尝试按日期比较两个日期。例如,我要检查学生是否在每个月的5日内支付了学费。

例如,该学生已在2019-01-13中注册,现在是2019-03-20,因此我想系统通知我,学生费用时间已过去7天。以及它应该自动检查下个月。

2 个答案:

答案 0 :(得分:0)

您可以定义任务计划

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

您可以根据自己的逻辑安排命令。它将在开始月份(01:00)的第5个日期触发

您的命令将类似于

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

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

答案 1 :(得分:0)

应该看起来像这样

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

该数组将包含超过其日期的用户