Tech Verse Logo
Enable dark mode
Laravel diffForHumans() Guide: Display Human-Readable Time Like a Pro

Laravel diffForHumans() Guide: Display Human-Readable Time Like a Pro

Md. Mostafijur RahmanMMd. Mostafijur Rahman

Md. Mostafijur Rahman

•1 min read

Introduction

When building modern web applications, displaying timestamps like 2026-05-01 14:32:11 isn’t user-friendly. Users prefer natural language such as:

  • “3 hours ago”

  • “5 minutes ago”

  • “just now”

Laravel solves this problem beautifully using Carbon’s diffForHumans() method.

In this guide, you’ll learn how to use and customize diffForHumans() for better UX and cleaner UI.

What is diffForHumans() in Laravel?

diffForHumans() is a method provided by Carbon (Laravel’s default date library) that converts timestamps into human-readable format.

Basic Example

$post->created_at->diffForHumans();

Output

3 hours ago

This is the simplest way to improve readability in your Laravel application.

Customize Time Output with Parts

By default, Laravel shows only one time unit. You can increase detail using the parts option.

$post->created_at->diffForHumans([
    'parts' => 2,
]);

Output

3 hours 12 minutes ago

👉 This is useful for blogs, dashboards, and analytics tools.

Advanced Formatting with Join

To make timestamps more visually appealing:

$post->created_at->diffForHumans([
    'parts' => 3,
    'join' => ' • ',
]);

Output

3 hours • 12 minutes • 45 seconds ago

👉 Perfect for activity feeds and modern UI designs.

Short Format for Better UX

For compact layouts (mobile, notifications):

$post->created_at->diffForHumans([
    'parts' => 3,
    'join' => ' • ',
    'short' => true,
]);

Output

3h • 12m • 45s ago

👉 This improves readability on smaller screens.

Best Practice: Use a Model Accessor

Avoid repeating logic across views by creating an accessor:

public function getHumanTimeAttribute()
{
    return $this->created_at->diffForHumans([
        'parts' => 2,
        'short' => true,
    ]);
}

Usage in Blade

{{ $post->human_time }}

Md. Mostafijur RahmanMMd. Mostafijur Rahman

WRITTEN BY

Md. Mostafijur Rahman

    Latest Posts

    View All

    Fixing Nginx Proxy Buffers to Prevent 502 Errors

    Fixing Nginx Proxy Buffers to Prevent 502 Errors

    Next.js Cache Tags and On-Demand Revalidation

    Next.js Cache Tags and On-Demand Revalidation

    Fixing Hydration Mismatches in React Components

    Fixing Hydration Mismatches in React Components

    Securing Next.js Route Handlers Against Abuse

    Securing Next.js Route Handlers Against Abuse

    Advanced Component State Colocation in React

    Advanced Component State Colocation in React

    Optimizing Linux Kernel Parameters for Nginx

    Optimizing Linux Kernel Parameters for Nginx

    Production Python Dependencies With UV and Lockfiles

    Production Python Dependencies With UV and Lockfiles

    Safe Tenant Isolation in Laravel Multi Database Apps

    Safe Tenant Isolation in Laravel Multi Database Apps

    Building Reliable LLM Fallback Chains with Python

    Building Reliable LLM Fallback Chains with Python

    Profiling Python Memory Usage with Memray

    Profiling Python Memory Usage with Memray