MySQL 来自数据库结果的 Laravel 格式 DateTime
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34224256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel format DateTime from database result
提问by quantatheist
My database stores two dates in the mysql DateTime format YYYY-MM-DD HH:MM:SS
. When I get this data (with other strings etc), I want to convert it to another format, maybe DD.MM.YYYY HH:MM:SS
and display it on my view in a table cell. My database dates are called date_begin
and date_end
.
我的数据库以 mysql DateTime 格式存储两个日期YYYY-MM-DD HH:MM:SS
。当我获得这些数据(以及其他字符串等)时,我想将其转换为另一种格式,也许DD.MM.YYYY HH:MM:SS
并在我的视图中的表格单元格中显示它。我的数据库日期被称为date_begin
和date_end
。
Better, when I get this dates from database, convert it to DD.MM.YYYY
format, separate the date and the time, store the time in a custom string ("HH1:MM1 - HH2:MM2") and bring both on my view.
更好的是,当我从数据库中获取此日期时,将其转换为DD.MM.YYYY
格式,将日期和时间分开,将时间存储在自定义字符串中(“HH1:MM1 - HH2:MM2”)并将两者都显示在我的视野中。
How can I achieve this? I found some examples to convert on the view, not in the controller, but I think this is not good for MVC.
我怎样才能做到这一点?我找到了一些在视图上转换的示例,而不是在控制器中,但我认为这对 MVC 不利。
回答by Bogdan
Not sure where you've gotten the impression that "formatting the date in the view is not good for MVC", because that's not a problemwhatsoever.
不确定您在哪里得到“在视图中格式化日期对 MVC 不利”的印象,因为这根本不是问题。
If you're using Eloquent Modelsyou can do it very easily:
如果你使用Eloquent 模型,你可以很容易地做到:
1.Add the columns to the $dates
property in your model class:
1.将列添加到$dates
模型类中的属性:
protected $dates = ['date_begin', 'date_end'];
This will ensure that the values get mutatedto Carbon instances.
2.In your view files you can use the format
method that Carbon offers like so:
2.在您的视图文件中,您可以使用format
Carbon 提供的方法,如下所示:
<!-- To use only the date with the given format -->
{{ $item->date_begin->format('Y.m.d') }}
<!-- To use only the time with the given format -->
{{ $item->date_begin->format('H:i:s') }}
<!-- To use both date and time with the given format -->
{{ $item->date_begin->format('Y.m.d H:i:s') }}
There's no need to split the value in time and date, just show what you want from the DateTime value using whatever formatyou want.
无需在时间和日期中拆分值,只需使用您想要的任何格式从 DateTime 值中显示您想要的内容。
If you're not using Eloquent models, then you can manually use Carbon to format your value like so:
如果您没有使用 Eloquent 模型,那么您可以手动使用 Carbon 来格式化您的值,如下所示:
{{ Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $item->date_begin)->format('Y.m.d') }}
回答by gtamborero
I have used the PHP solution without Carbon:
我使用了没有 Carbon 的 PHP 解决方案:
Get the database date as you do (with the default format: YYYY-MM-DD HH:MM:SS) and when you print it on your view replace $item->date_begin
to:
获取数据库日期(使用默认格式:YYYY-MM-DD HH:MM:SS),并在视图上打印时替换$item->date_begin
为:
date('d-m-Y H:i:s', strtotime($item->date_begin))
After that, when you save it on the database add on the DB update:
之后,当您将其保存在数据库中时,请添加数据库更新:
date('Y-m-d H:i:s', strtotime($request->date_begin))