Laravel Carbon diffForHumans() Example

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Hi Dev,
So in this article i will cover that, how to implement laravel carbon diffforhumans example, now this article goes in detailed on laravel carbon diffforhumans language ,now 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.

Now we will see simple example of diffForHumans() of carbon in laravel 6, laravel 7 and laravel 8 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"

Read Also : How to send mail using mailgun in laravel?

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. Also you can follow us on Facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × three =