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

noblemfd's avatar

How to convert time in seconds to human readable

In my Laravel-8, I have this in my view blade:

{{$duration['time_spent'] ?? '' }}

time_spent is in seconds. How do I convert it to hours in human readable

For instance, 4 Hours 20 Minutes

time_spent is something like: 124323.4

When I did

{{$duration['time_spent']->diffForHumans() ?? '' }}

I got this error:

Call to a member function diffForHumans() on float

How do I resolve it?

Thanks

0 likes
9 replies
piljac1's avatar

You can use CarbonInterval and convert it for humans. The cascade method converts seconds to days, hours, minutes.

CarbonInterval::seconds($duration['time_spent'])->cascade()->forHumans();
2 likes
noblemfd's avatar

@piljac1 - When I did:

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

I got the error:

[previous exception] [object] (Error(code: 0): Class 'CarbonInterval' not found

MichalOravec's avatar

@noblemfd

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

or add it to the alias array in config/app.php

'CarbonInterval' => Carbon\CarbonInterval::class,
piljac1's avatar

You should either alias it like @michaloravec suggested, or if you want better autocompletion (at least on VS Code), you can add this to the top of your file

use Carbon\CarbonInterval;

or prefix the namespace on the code line if it's a one time thing.

piljac1's avatar

You're right, I thought it was in a class, I missed the part where he said it was in a controller. Either way, I don't think you should have that kind of logic directly in your blade. It should be in the appropriate model or class.

BRVK's avatar

$min = $seconds / 60;

$worked_hours = intdiv($min, 60).':'. ($min % 60).' min';

output:- 8:46 min

I am using this type in my blade page.

It may help.

Please or to participate in this conversation.