Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alexmansour's avatar

Getting the sum of hours between two DATETIME columns

Hi all,

I'm trying to execute the following query to get the sum of hours between two datetime columns based on some cretriea, but the problem that I'm getting null result.

$numberOfHours = DB::table('session')->where('doctor_id', $doctorId) ->whereNotNull('start_time') ->whereBetween('end_time', [$start, $end]) ->select(DB::raw("SUM(TIMESTAMPDIFF(HOUR, 'start_time', 'end_time')) as result"))->get(['result']);

Any suggestions?

Thanks.

0 likes
1 reply
alexmansour's avatar
alexmansour
OP
Best Answer
Level 1

I have fixed the issue, here is the updated query:

$numberOfHours = DB::table('session')->where('doctor_id', $doctorId) ->whereNotNull('start_time') ->whereBetween('end_time', [$start, $end]) ->select(DB::raw("SUM(time_to_sec(timediff(end_time, start_time)) / 3600) as result"))->get(['result']);

2 likes

Please or to participate in this conversation.