Laravel 9 Carbon diffForHumans() Example Tutorial

10-Apr-2023

.

Admin

Laravel 9 Carbon diffForHumans() Example Tutorial

Hi Dev,

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

Here, in laravel 9 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 of carbon diff for humans laravel 9 Code so follow my example code and you can use your laravel 9 application to copy that code.

Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app

Example : 1

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Carbon\Carbon;

class HomeController extends Controller

{

/**

* laravel 9 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 9 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 9 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 9 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 9 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 9 current diffforhumans...

*

* @return string

*/

public function index()

{

$result = Carbon::now()->subDays(24)->longAbsoluteDiffForHumans();

dd($result);

}

}

Output:

"3 weeks"

It will help you...

#Laravel 9