用于比较时间的 Laravel Carbon plus 或分钟一分钟

问题描述

我正在处理通知,然后每分钟检查一次预定的通知。它从不调用它,因为我的 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
        }
     }

参考:
whereBetweenCarbon

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...