laravel carbon calculate age

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
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

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →