Laravel Age Calculate From Date Using Carbon


Share:
Published By: Admin, Published On: Aug 03, 2021, Category: Laravel 8 Laravel


Hi Artisan,

Today,In this example i will share you how to age calculate from date using carbon in laravel so it can easy to use in laravel app.

Sometimes if you work web application then you might be needed to age calculate from date of birth. Like your birth date is something around 15/08/1997(dd/mm/yyyy) and today date like 03/08/2021(dd/mm/yyyy) then it should be return 23 years old you are.

Here, I will give you full example for how to age calculate from date using carbon in laravel so follow my example code and you can use your laravel application copy that code.

Example
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * calculate age from date..
     *
     * @return string
    */
    public function index(Request $request)
    {
        $dateOfBirth = '1997-08-15';
        $years = Carbon::parse($dateOfBirth)->age;
            
        dd($years);
    }

}
Output
23

I Hope It Will Help You..

Share: