laravel carbon calculate age

10-Apr-2023

.

Admin

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"

I hope it can help you...

#Laravel 7

#Laravel

#PHP

#Laravel 6