Skip to content

All topics  /  Laravel

laravel carbon calculate age

Laravel ·

Hi Guys,

In this example,I will learn you how to calculate carbon age in laravel.you can simply to calculate in laravel.

your birth date is around 1999-12-20 and today date like 2019-12-23 then it should be return 20 years old you are.So just see example

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);
}

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"