Laravel Carbon diffForHumans() Example

By Hardik Savani April 16, 2024 Category : Laravel

Hi Dev,

In this article we will cover on how to implement laravel carbon diffforhumans example. This article goes in detailed on laravel carbon diffforhumans language. I’m going to show you about laravel carbon diffForHumans(). if you want to see example of carbon difference for humans laravel then you are a right place.

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

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:

Let's see one by one example:

Example 1:

<?php

namespace App\Http\Controllers;

use Carbon\Carbon;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$myDate = '12/08/2020';

$result = Carbon::createFromFormat('m/d/Y', $myDate)->diffForHumans();

var_dump($result);

}

}

Output:

string(11) "2 weeks ago"

Example 2:

<?php

namespace App\Http\Controllers;

use Carbon\Carbon;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$myDate = '12/08/2020';

$myDate2 = '12/10/2020';

$newDate = Carbon::createFromFormat('m/d/Y', $myDate2);

$result = Carbon::createFromFormat('m/d/Y', $myDate)->diffForHumans($newDate);

var_dump($result);

}

}

Output:

string(13) "2 days before"

I hope it can help you...

Tags :
Shares