如何在 laravel 上获得带有碳的生日(年、月、日)?

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

我想将生日格式转换为带碳的年月日格式 laravel 5.4

根据 carbon 的文档,获取 blade 年龄的方法是这样

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

但结果是“0”,我想要这个

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

谢谢你,对不起我的英语。

碳的age return就是一个人年龄中的年数,如你所见。

在您的模型中,您可以设置一个 getAge 方法,该方法将 return 以年、月和日为单位的年龄:

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

您可以在视图中将您的模型称为 $user->getAge()