Laravel 9 Carbon diffForHumans() Example Tutorial
Teams applying “Laravel 9 Carbon diffForHumans() Example Tutorial” in a production project can use the official example to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
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...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial