php - Laravel Carbon 加或分钟一分钟用于比较时间
问题描述
我正在处理通知,然后每分钟检查一次预定通知。它从不调用它,因为我的 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) {
推荐阅读
- snowflake-cloud-data-platform - 如何使用 DBT 使用 sso 身份验证连接雪花
- webhooks - 传入的 Slack Webhook URL 组件
- python - Whoosh:如何将搜索摘录作为列表获取
- oauth - UiPath Cloud 使用什么类型的授权?
- python - 如何使用来自 github 的 python 库
- react-native - axios 取消的请求仍显示在 ngrok 中
- ios - 试图快速删除从firebase检索到的数组中的重复值
- django - Django - 由于缺少字段错误,我无法进行迁移
- react-native - React Native - 原生驱动动画 - 高度
- reactjs - 警告:React 检测到由 null 调用的 Hook 的顺序发生了变化