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

Ligonsker's avatar

Custom time formatting function for Blade files - where should I place it?

Hello,

I need to reuse the following function that converts number of seconds to a Days:Hours:Minutes:Seconds format.

And I need to use it in blade files

This is the function I made:

function format_total_time($total_seconds) {
    $days = floor($total_seconds / 86400);
    $hours = floor(($total_seconds - ($days * 86400)) / 3600);
    $minutes = floor(($total_seconds / 60) % 60);
    $seconds = $total_seconds % 60;

    $days = $days < 10 ? "0$days" : $days;
    $hours = $hours < 10 ? "0$hours" : $hours;
    $minutes = $minutes < 10 ? "0$minutes" : $minutes ;
    $seconds = $seconds < 10 ? "0$seconds" : $seconds;

    $total_time_string = $days . ":" . $hours . ":" . $minutes . ":" . $seconds;
    return $total_time
}

What is a good convention to do it so that I can easily use it in Blade files?

I remember someone here told me Helpers should rarely ever used so I want to use a better practice

Thanks

0 likes
4 replies
Polkiujhy's avatar

php artisan make:provider YourFileNameProvider

in provider

public function register()
 {
     require_once base_path().'/app/YourFolder/YourFileNameProvider.php';
 }

Register your provider into App\Config\app.php wihtin the providers

'providers' => [

     /*
     * Laravel Framework Service Providers...
     */
     Illuminate\Auth\AuthServiceProvider::class,
     ...
     Illuminate\Validation\ValidationServiceProvider::class,
     Illuminate\View\ViewServiceProvider::class,
     App\Providers\YourFileNameProvider::class, //Add your service provider

php artisan clear-compiled

php artisan config:cache

and finaly you can use your values :)

1 like
Ligonsker's avatar

@Polkiujhy thanks. and how will the usage be?

Because what I did now was to use the @inject at the top of the blade file, like so:

I have a Helpers class:

class Helpers {
    public function format_total_time($total_seconds) {
        $days = floor($total_seconds / 86400);
        $hours = floor(($total_seconds - ($days * 86400)) / 3600);
        $minutes = floor(($total_seconds / 60) % 60);
        $seconds = $total_seconds % 60;

        $days = $days < 10 ? "0$days" : $days;
        $hours = $hours < 10 ? "0$hours" : $hours;
        $minutes = $minutes < 10 ? "0$minutes" : $minutes ;
        $seconds = $seconds < 10 ? "0$seconds" : $seconds;

        $total_time_string = $days . ":" . $hours . ":" . $minutes . ":" . $seconds;
        return $total_time
    }
}

Then in Blade:

@inject('helpers', 'App\Helpers\Helpers')
@extend
   ...

   {{ $helpers->format_total_time($seconds) }}
1 like
Polkiujhy's avatar

@Ligonsker now delete your function and use carbon in blade like this: :D

Carbon\CarbonInterval::seconds($seconds)->cascade()->forHumans();

Snapey's avatar

is this across many controllers?

Please or to participate in this conversation.