Search code examples
phplaravelphp-carbon

How will I get a birthdate (year,month,day) with carbon on laravel?


I want to convert a birthdate to format year, month, day with carbon on laravel 5.4

according the carbon's documentation the method to get an age on blade is this

Carbon\Carbon::parse($bday)->age

but the result is '0' and i want this

$bday =  '2017-10-01' =  0 year 1 month 4 days

Thank you and sorry my English.


Solution

  • Carbon's age returns just the number of years in a person's age, as you have seen.

    In your model, you could set up a getAge method that will return the age in years, months and days:

    public function getAge(){
        return $this->birthdate->diff(Carbon::now())
             ->format('%y years, %m months and %d days');
    }
    

    You can call your model in the view as $user->getAge()