首页 > 解决方案 > 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.

标签: phpmysqlsqllaravellaravel-5.5

解决方案


可能是因为您使用了 whereDate。whereDate 方法可用于将列的值与日期进行比较。参考:https ://laravel.com/docs/5.8/queries -> whereDate。我认为你应该只使用 where 而不是 whereDate。这将为您提供当前输出。


推荐阅读