0

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

Jakub
  • 1,260
  • 1
  • 13
  • 40
  • 1
    Does this answer your question? [PHP: convert seconds to minutes and hours](https://stackoverflow.com/questions/35960906/php-convert-seconds-to-minutes-and-hours) – Nico Haase Sep 06 '22 at 12:04
  • ooh I will try that in the model, maybe that will work! – Jakub Sep 06 '22 at 12:05
  • Yea idk what im doing, i got `App\Models\Task::timeTrackedForHumans must return a relationship instance.` when i tried to retun a s tring – Jakub Sep 06 '22 at 12:18
  • public function timeTrackedForHumans() { $seconds = '555'; return $seconds; } – Jakub Sep 06 '22 at 12:18
  • I'm too embarrassed to formulate that as an answer: $hours = (int)($tracked_time/3600); $minutes = (int)($tracked_time%3600/60); – jspit Sep 06 '22 at 12:56

1 Answers1

0

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) }}
Jakub
  • 1,260
  • 1
  • 13
  • 40