⌘K
Noting Found

View all related articles

Convert Seconds to Minutes, Hours, Using Carbon & Laravel: The Easy Way!

Tips & Tricks 1 min read

Table of Content

    Introduction

    Ever stared at a number representing time in seconds and wished it were more human-friendly? Maybe you're tracking video playback duration or analyzing task completion times. Raw seconds don't always provide the most intuitive understanding. Luckily, Laravel, a popular PHP framework, offers built-in tools to simplify this conversion.

    This article delves into converting seconds into a human-readable format using Laravel and its companion library, Carbon. Carbon empowers you to manipulate dates and times with ease. We'll explore two practical methods to achieve this conversion, making it a breeze to present time durations in a clear and concise way.

    Using Carbon Instance

    You can humanize the time from the seconds by the following

    1use Carbon\Carbon;
    2 
    3...
    4 
    5$seconds = 'your seconds';
    6 
    7// pass the seconds
    8$duration = Carbon::now()->subSeconds($seconds);
    9// create a placeholder
    10$placeholder = [
    11 'h' => $duration->format('H'),
    12 'm' => $duration->format('i'),
    13 's' => $duration->format('s')
    14];
    15 
    16// return the result
    17return __(':hh :mm :ss', $placeholder);

    Using CarbonInterval Instance

    If you want an easy way to show the duration, you can also use CarbonInterval

    1use Carbon\CarbonInterval;
    2 
    3...
    4 
    5$seconds = 'your seconds';
    6$interval = CarbonInterval::seconds($seconds);
    7 
    8return $interval->cascade()->forHumans(['short' => true]);

    Using It Everywhere

    If you want to use this method in your Laravel application, in multiple places. You may use it as a helper function, or as a blade directive.

    Creating Helper Method

    1use Carbon\CarbonInterval;
    2 
    3function humanize($seconds): string {
    4 $interval = CarbonInterval::seconds($seconds);
    5 
    6 return $interval->cascade()->forHumans(['short' => true]);
    7}

    Creating Custom Blade Directive

    In your AppServiceProvider or your custom ServiceProvider, use this code inside the boot method

    1use Carbon\CarbonInterval;
    2use Illuminate\Support\Facades\Blade;
    3 
    4public function boot()
    5{
    6 Blade::directive('humanizeDuration', function ($seconds) {
    7 // you can eaither return the created helper method
    8 return humanize($seconds);
    9 
    10 // or you can re-create the method here
    11 $interval = CarbonInterval::seconds($seconds);
    12 
    13 return $interval->cascade()->forHumans(['short' => true]);
    14 });
    15}

    Conclusion

    The methods presented in this article provide both flexibility and reusability. Choose the approach that best aligns with your project structure and coding style. With these techniques at your disposal, you can effectively communicate time-related information to your users in a clear and intuitive manner.

    Ok, that's it for today, we'll meet in another article.

    Happy Coding :)

    Related Tags

    About the Author

    Oussama's Profile Picture
    Oussama
    Full Stack Web Developer | Technical Writer

    Oussama is an experienced full-stack web developer with a strong focus on Laravel. He's passionate about crafting web applications with Filament and the TALL Stack. With 8+ years of experience, and he's a dedicated open-source contributor.

    Comments

    Join our newsletter

    Subscribe to Our Newsletter and never miss our offers, latest news, Articles, etc.

    We care about the protection of your data. Read our Privacy Policy.