06-Jun-2023
.
Admin
Hi Dev,
In This example, I will learn how to work diffforhumans in Laravel 10 using carbon in Laravel 10 so it can be easy to use in Laravel 10 app.
Here, in Laravel 10 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 10 Code so follow my example code and you can use your Laravel 10 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 the 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 10 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 10 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 10 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 10 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 10 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 10 current diffforhumans...
*
* @return string
*/
public function index()
{
$result = Carbon::now()->subDays(24)->longAbsoluteDiffForHumans();
dd($result);
}
}
Output:
"3 weeks"
It will help you...
#Laravel 10