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

dilfdo's avatar

calculating total hours from times laravel sql

how to calculate total hours from number of rows using laravel

time_sheet_id     shift_start_time     shift_end_time

35                  08:00:00            10:00:00

35                  10:00:00            13:00:00

35                  13:00:00            15:00:00

36                  13:00:00            15:00:00

36                  13:00:00            15:00:00


36                  13:00:00            15:00:00


i want to calculate no of hours for each timesheet_id . pls advice

0 likes
14 replies
spyworld's avatar

Use Carbon

$time = new Carbon($shift_start_time);
$shift_end_time =new Carbon($shift_end_time);
$time->diffForHumans($shift_end_time);
devtraining's avatar

row sql: SELECT time_sheet_id, sum( TIMEDIFF( shift_end_time, shift_start_time ) ) FROM yourtable

dilfdo's avatar

@devtraining : this give result as string if sum value is 10 hours and 30minutes it shows as 103000 pls advice

devtraining's avatar

It is safe as you made it safe, it does not have parameters and if you need them you could use binding.

devtraining's avatar

@dilfdo this should fix your problem: SELECT time_sheet_id, time(sum( TIMEDIFF( shift_end_time, shift_start_time ) )) FROM yourtable

dilfdo's avatar

@devtraining : im getting following error

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT time(sum(TIMEDIFF( shift_end_time, shift_start_time ))) from `shifts` whe' at line 1 (SQL: select SELECT time(sum(TIMEDIFF( shift_end_time, shift_start_time ))) from `shifts` where `time_sheet_id` = 29)


                $no_of_hours = DB::Table('shifts')
                    ->where('time_sheet_id','=', $timesheet_id->id)
                     ->selectRaw('SELECT time(sum(TIMEDIFF( shift_end_time, shift_start_time )))')
                    ->get();
sabuncuserhat's avatar

@dilfdo just remove 'SELECT' in your query. It should be like this;

$no_of_hours = DB::Table('shifts')
        ->where('time_sheet_id','=', $timesheet_id->id)
        ->selectRaw('time(sum(TIMEDIFF( shift_end_time, shift_start_time ))) as total')
        ->get();

    return $no_of_hours;
1 like
kfirba's avatar

@dilfdo you are looking for a query like:

select
time_sheet_id, 
SUM(TIME_TO_SEC(TIMEDIFF(shift_start_time, shift_time_end))) as shift_length
from your_table_name
group by time_sheet_id

This will return the total shift length per shift_id in seconds. Something like:

  • 35 - 17805
  • 36 - 28904

You can just divide the result by 3600 for hours.

I hope this is what you were looking for.

1 like
ka.hazem's avatar

@spyworld what you wrote is for calculating time diff for each time_sheet_id,

what am trying to do is to sum all these hours, so i get total working hours for example.

would you please help with this ?

Thanks in advance

jlrdw's avatar

Do the above then add results.

djdiramio's avatar

If you follow the advice above and get the total number of hours for each timesheet, your result set should be a collection which you can then just use the sum() method on to sum the results.

Remember too that if you have multiple users tracking time sheets, you'll need to take that into consideration and include a user_id field in your results.

Please or to participate in this conversation.