Home Laravel laravel carbon calculate age

laravel carbon calculate age

by Shahriar Sagor
laravel carbon calculate age

Hello Dev’s
Today now in this example,I will show you how to calculate carbon age in your laravel application. So we can simply to calculate in laravel application.

Your birth date is around 1995-05-20 and today date like 2021-04-27 then it should be return 26 years old you are. Now so just see example bellow:

Example 1 :
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
    $dateOfBirth = '1999-12-20';
    $years = \Carbon\Carbon::parse($dateOfBirth)->age;
      
    dd($years);
}

Now Output

20  //years
Example 2 :
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
    $dateOfBirth = '1999-12-20';
    $years = \Carbon\Carbon::parse($dateOfBirth)->diff(\Carbon\Carbon::now())->format('%y years, %m months and %d days');
      
    dd($years);
}

Output

"20 years, 0 months and 3 days"

Read Also : Laravel Toastr Notifications Example

I hope it will help you. Also you can follow us on Facebook

close

You may also like