Laravel Carbon diffForHumans() Example Tutorial


Share:
Published By: Admin, Published On: Oct 06, 2021, Category: PHP Laravel 8 Laravel


Hi Dev,

In This example, I will learn you how to work diffforhumans in laravel using carbon in laravel so it can easy to use in laravel app.

Here, in laravel carbon provided it is easier for humans to read 1 month ago compared to 30 days ago This is a common function seen in most date libraries so I thought I would add it here as well. The lone argument for the function is the other Carbon instance to diff against, and of course, it defaults to now() if not specified.

Here, I will give you a full example for carbon diff for humans laravel Code so follow my example code and you can use your laravel application to copy that code.

Example : 1
<?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 = '09/06/2021';
        $result = Carbon::createFromFormat('m/d/Y', $myDate)->diffForHumans();

        dd($result);
    }

}
Output
"1 month ago"
Example : 2
<?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/2021';
        $myDate2 = '06/10/2021';
          
        $newDate = Carbon::createFromFormat('m/d/Y', $myDate2);
        $result = Carbon::createFromFormat('m/d/Y', $myDate)->diffForHumans($newDate); 

        dd($result);
    }

}
Output
"2 months after"
Example : 3
<?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
<?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
<?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
<?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"

It Will Help You..

Share: