Laravel Carbon: Convert seconds into HH:MM

Viewed 41

Problem:

$tracked_time = 994554

In the laravel blade view, I want this to display as 77h 55m

I tried to find a few solutions

{{ Carbon\CarbonInterval::seconds($duration['tracked_time'])->cascade()->forHumans()  ?? '' }}

it displays days and seconds, along with spelling it out.

1 day 7 hours 53 minutes 25 seconds

I also found

gmdate("H:i:s", $seconds);

But that doesn't work when its more than 1 day of seconds.

I tried to use the ->format('H:i:s') on it but it just broke it.

I think the proper way would be format this in the Model with something like

public function timeTrackedForHumans()
{
 return $this->time_tracked->dosometing();

}

So that I could call it like $task->timeTrackedForHumans

Any advice is much appreciated. Is this something that always goes into Model or is it fine to also include it in the blade? Or if anyone knows what I can search for to find like a bunch of examples of how to do these types of formatting functions in the models.

I know this is a big ask and prob a super novice one, but genuinely am lost with this

1 Answers

I was able to get this working in the blade with

{{ sprintf('%dh %dm', $task->time_tracked_in_seconds / 3600, floor($task->time_tracked_in_seconds / 60) % 60) }}
Related