问题描述
我正在处理通知,然后每分钟检查一次预定的通知。它从不调用它,因为我的 Now 变量采用分钟格式 (2021-01-28 10:27),其中存储数据库 send_date (2021-01-28 10:15:11)
$Now = date("Y-m-d H:i",strtotime(Carbon::Now()));
$notifications = Notification::get();
if($notifications !== null)
{
Log::info('Notifications are not null ' .$Now); // shows in log (2021-01-28 10:27)
$notifications->where('send_date',$Now)->each(function($message) {
Log::info('looking at send_date'); // never shows in log
}
}
或者还有其他方法可以做到这一点,而我没有看到?
解决方法
那么你可以用下面的方式
$notifications->whereBetween('send_date',[Carbon::now()->startOfMinute(),Carbon::now()->endOfMinute()])->each(function($message) {
如果您需要自己格式化时间,则:
$notifications->whereBetween('send_date',[Carbon::now()->startOfMinute()->format('Y-m-d H:I:s'),Carbon::now()->endOfMinute('Y-m-d H:I:s')])->each(function($message) {
,
$notifications = Notification::get();
if($notifications !== null)
{
Log::info('Notifications are not null ' .$now); // shows in log (2021-01-28 10:27)
$notifications->whereBetween('send_date',[Carbon::now()->subMinute()->format('Y-m-d H:i'),Carbon::now()->addMinute()->format('Y-m-d H:i')])->each(function($message) {
Log::info('looking at send_date'); // never shows in log
}
}