【问题标题】:How will I get a birthdate (year,month,day) with carbon on laravel?我如何在 laravel 上用碳获得生日(年、月、日)?
【发布时间】:2017-11-05 16:35:12
【问题描述】:

我想在 laravel 5.4 上用碳将生日转换为年、月、日格式

根据碳的文档,获得刀片年龄的方法是这样的

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

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

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

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

【问题讨论】:

    标签: php laravel php-carbon


    【解决方案1】:

    Carbon 的 age 只返回一个人的年龄,如您所见。

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

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

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

    【讨论】:

      猜你喜欢
      • 2020-02-07
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多