我在 MySQL 数据库中有一个名为 birth_date
的列,类型为 date
。
现在,我想以这种格式在我的 View 中显示它:2008 年 5 月 23 日
我试过:
{{ $user->user_information->birth_date->format('m-d-Y') }}
它显示:
ErrorException (E_ERROR) Call to a member function format() on string
我也试过
{{ date('m-d-Y', $user->user_information->baptism_date) }}
它显示:
ErrorException (E_ERROR) A non well formed numeric value encountered
任何人都可以用正确的格式化代码帮助我吗?
最佳答案
为此,您可以使用 date()
函数,例如:
date('m-d-Y', strtotime($user->user_information->birth_date));
或者您也可以使用 Carbon
来达到这个目的,例如:
{{ \Carbon\Carbon::parse($user->user_information->birth_date)->format('m-d-Y')}}
2008 年 5 月 23 日:
date('M d, Y', strtotime($user->user_information->birth_date));
工作代码 snippet
date()
格式列表可用here
M
- 一个月的简短文本表示,三个字母
关于mysql - 从数据库 Laravel 格式化日期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54307687/