trafeka
trafeka

Reputation: 1

Summary minutes with hours in Laravel

I am beginner in Laravel. I make my project in Laravel 5.8. I have minutes and hours in DB.

I have this code:

$workedHours = WorkingTime::where('case_id', $request->id)->sum('hours_worked');
$workedMinutes = WorkingTime::where('case_id', $request->id)->sum('minutes_worked');

Example result:

$workedHours = 438;
$workedMinutes = 483;

I need to add hours over minutes and display the total. For example, 44:12 (44 hours and 12 minutes).

How can i make this?

Upvotes: 0

Views: 57

Answers (1)

mmabdelgawad
mmabdelgawad

Reputation: 2545

You can do

$workedHours = 438;
$workedMinutes = 483;

$workedHoursInMinutes = $workedHours * 60;
$totalMinutes = $workedHoursInMinutes + $workedMinutes;

$hours = floor($totalMinutes / 60);
$minutes = ($totalMinutes % 60);

dd(sprintf('%02d:%02d', $hours, $minutes));

The result will be 446:03

Upvotes: 1

Related Questions