php - Wrong addition of hours to a MySQL table column timestamp Laravel
问题描述
I need to get 3 types of records in order to send reminders to those ones, first I need to send reminders after 24 hours that the records were created, then after 48 hours and finally after 72 hours.
I have done this so far but it doesn't seem to work properly because, assume that a record was created at 2019-06-10 19:00:00
when the current day turns to 2019-07-10 00:01:00
it seems to consider as if 24 hours have already passed:
$fecha_hoy = Carbon::now();
$solicitudes_24 = SolicitudUser::whereDate(DB::raw('DATE_ADD(created_at, INTERVAL 24 HOUR)'), '<', $fecha_hoy)
->whereDate(DB::raw('DATE_ADD(created_at, INTERVAL 48 HOUR)'), '>', $fecha_hoy)
->whereNull('recordatorio_estado')
->whereHas('curso', function($query){
$query->whereHas('operativo', function ($q){
$q->whereDate('dateOfertaAcademicaOperativoFechaRegistroFin', '>', Carbon::now());
});
})
->where(function($query){
$query->where('registro_completo', 0)
->orWhereNull('registro_completo');
})
->get();
$solicitudes_48 = SolicitudUser::whereDate(DB::raw('DATE_ADD(tblusersSolicitudes.created_at, INTERVAL 48 HOUR)'), '<', $fecha_hoy)
->whereDate(DB::raw('DATE_ADD(tblusersSolicitudes.created_at, INTERVAL 72 HOUR)'), '>', $fecha_hoy)
->where(function($query){
$query->where('recordatorio_estado', 1)
->orWhereNull('recordatorio_estado');
})
->whereHas('curso', function($query){
$query->whereHas('operativo', function ($q){
$q->whereDate('dateOfertaAcademicaOperativoFechaRegistroFin', '>', Carbon::now());
});
})
->where(function($query){
$query->where('registro_completo', 0)
->orWhereNull('registro_completo');
})
->get();
$solicitudes_72 = SolicitudUser::whereDate(DB::raw('DATE_ADD(tblusersSolicitudes.created_at, INTERVAL 72 HOUR)'), '<', $fecha_hoy)
->where(function($query){
$query->where('recordatorio_estado', 2)
->orWhereNull('recordatorio_estado');
})
->whereHas('curso', function($query){
$query->whereHas('operativo', function ($q){
$q->whereDate('dateOfertaAcademicaOperativoFechaRegistroFin', '>', Carbon::now());
});
})
->where(function($query){
$query->where('registro_completo', 0)
->orWhereNull('registro_completo');
})
->get();
How can I solve this?
PD. You can ignore the rest of the query chain that doesn't involve the dates.
解决方案
可能是因为您使用了 whereDate。whereDate 方法可用于将列的值与日期进行比较。参考:https ://laravel.com/docs/5.8/queries -> whereDate。我认为你应该只使用 where 而不是 whereDate。这将为您提供当前输出。
推荐阅读
- matlab - 在矩阵中添加索引列
- bash - 使用代理 Ubuntu-16.04 在 azure-devops 管道 bash cake 任务中获取 ##[error] Bash 并以代码“1”退出
- python - 在同一张图上用彩色图绘制多列数据框
- c - 如果缺少某些字段,我想知道如何存储以分号分隔的文本的输入
- javascript - 如何将图像 URI 转换为字节博览会
- react-native - KeyBoard 避免使用绝对位置元素查看视图
- ruby - OctoKit Ruby 身份验证
- reactjs - 单击时不显示模态(React)
- c# - Ocelot .NET Core 平衡问题与已关闭的微服务
- firebase - Firestore 如果文档不存在则创建文档,如果存在则跳过