How To Use Laravel DiffForhumans With Simple Examples

Laravel diffforhumans – Brillian Solution. Hy everyone.

We will see simple example of diffForHumans() of carbon in laravel 6, laravel 7, laravel 8 and laravel 9 application.

Laravel DiffForhumans

Carbon Laravel

Introduction

The Carbon class is inherited from the PHP DateTime class.

<?php
namespace Carbon;

class Carbon extends \DateTime
{
    // code here
}


You can see from the code snippet above that the Carbon class is declared in the Carbon namespace. You need to import the namespace to use Carbon without having to provide its fully qualified name each time.

use Carbon\Carbon;

Examples in this documentation will assume you imported classes of the Carbon namespace this way.

If you’re using Laravel, you may check our Laravel configuration and best-practices recommendations.

If you’re using Symfony, you may check our Symfony configuration and best-practices recommendations.

We also provide CarbonImmutable class extending DateTimeImmutable. The same methods are available on both classes but when you use a modifier on a Carbon instance, it modifies and returns the same instance, when you use it on CarbonImmutable, it returns a new instances with the new value.

Read More : Mengenal Carbon di Laravel

Laravel carbon diffForHumans() return humans to read “1 month ago” compared to “30 days” ago. so you can easily get humans read time from current date and given date. let’s see both examples:

Example 1 – Laravel Diffforhumans:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon diffforhumans...
     *
     * @return string
    */
    public function index()
    {
        $myDate = '10/09/2022';
        $result = Carbon::createFromFormat('m/d/Y', $myDate)->diffForHumans();
        dd($result);
    }
}

Output:

“1 Month ago”

Example 2 – Laravel Diffforhumans

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon diffforhumans...
     *
     * @return string
    */
    public function index()
    {
        $myDate = '08/10/2022';
        $myDate2 = '06/10/2022';
          
        $newDate = Carbon::createFromFormat('m/d/Y', $myDate2);
        $result = Carbon::createFromFormat('m/d/Y', $myDate)->diffForHumans($newDate); 
        dd($result);
    }
}

Output:

2 months after

Example 3 – Laravel Difforhumans:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon diffforhumans...
     *
     * @return string
    */
    public function index()
    {
        $result = Carbon::now()->diffForHumans(Carbon::now()->subYear());
        dd($result);
    }
}

Output:

“11 months after”

Example 4 – Laravel DiffForhumans:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel current diffforhumans...
     *
     * @return string
    */
    public function index()
    {
        $newDate = Carbon::now()->addSeconds(5)->diffForHumans();
        dd($newDate);
    }
}

Output:

“4 seconds from now”

Example 5 – Laravel DiffForhumans:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel current diffforhumans...
     *
     * @return string
    */
    public function index()
    {
        $result = Carbon::parse('2021-09-01')->diffForHumans('2021-09-20');
        dd($result);
    }
}

Output:

“2 weeks before”

Example 6 – Laravel DiffForhumans:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel current diffforhumans...
     *
     * @return string
    */
    public function index()
    {
        $result = Carbon::now()->subDays(24)->longAbsoluteDiffForHumans();
        dd($result);
    }
}

Output:

“3 weeks”

Thats all example laravel difforhumans carbon laravel. I hope It will help you for Carbon Laravel.

brillian

Leave a Reply

%d bloggers like this: