2014-09-30 45 views
0

如何创建一个可读的生日,如1990年1月1日在laravel中使用碳。碳创建可读生日

在我user.php的(模型)我有我的数据库

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

得到一个生日列的函数如何从MySQL数据库得到的生日,并返回它像1990年1月1日或像你的生日等待1个月之前等等等等等等。 :d

感谢

回答

2

的可靠方式:

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; 
    } 
} 

您需要增加一些逻辑(打印日期,打印DIFF或其他),它取决于你如何存储的生日,在分贝。

+0

1990年1月1日如何? – yaeykay 2014-09-30 07:36:50

+0

return \ Carbon \ Carbon :: parse($ birthday) - > toDateTimeString(); 结果:1990-01-01 00:00:00 – yaeykay 2014-09-30 07:40:27

+0

' - > format('F j,Y');' - 阅读https://github.com/briannesbitt/Carbon和http://的文档php.net/manual/en/class.datetime.php – 2014-09-30 07:41:16