简体   繁体   English

碳创造可读的生日

[英]Carbon Create Readable Birthday

how to create a readable birthday like January 1, 1990 using carbon in laravel. 如何使用Laravel中的碳创建可读的生日(如1990年1月1日)。

In my User.php (Model) I have a function that get a birthday column in my database 在我的User.php(模型)中,我有一个在数据库中获取生日列的函数

public function getBirthdayAttribute($birthday)
{
    return $this->attributes['birthday'] = \Carbon\Carbon::????($birthday);
}

How can I get the birthday from mysql database and return it like January 1, 1990. Or something like 1 month to go before your birthday blah blah blah. 我怎么能从mysql数据库中获取生日并像1990年1月1日那样返回它呢?或者像是在生日前1个月之类的东西。 :D :D

Thanks 谢谢

The reliable way: 可靠的方法:

public function getBirthdayAttribute($birthday)
{
    Try {
      return \Carbon\Carbon::parse($birthday)->diffForHumans(); // 8 months ago / 1 month from now etc
      // or:
      // ->format('F j, Y'); // returns eg. January 1, 2000
    }
    catch (\Exception $e)
      return $birthday;
    }
}

You need to add some logic (print date, print diff or whatever), and it depends on how you store the birthday in the db. 您需要添加一些逻辑(打印日期,打印差异或其他内容),这取决于您如何在数据库中存储生日。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM