[英]Carbon - Comparing two dates by their days
最近,我試圖按日期比較兩個日期。 例如,我想檢查一下學生是否在每個月的5日內支付了學費。
例如,學生在2019-01-13
注冊,現在是2019-03-20
因此我想系統地通知我,學生費用時間已過去7天。 以及它應該自動檢查下個月。
您可以定義任務計划
$schedule->command('student:fees_notify')->monthlyOn(5, '01:00');
您可以根據自己的邏輯安排命令。 它將在月份開始的第5個日期(01:00)觸發
您的命令看起來像
class StudentFeesNotify extends Command
{
protected $signature = 'student:fees_notify';
protected $description = 'your desc.....';
public function handle()
{
// you can write your logic here
}
}
它應該看起來像這樣
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;
}
});
該數組將包含超過其日期的用戶
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.