laravel 在控制器中格式化日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36848231/
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
Format Date In Controller
提问by Lincoln Binda
I have the following form :
我有以下表格:
<div class="form-group col-md-12 col-sm-12 col-xs-12">
<div class="col-md-2 col-sm-2 col-xs-2 form-label">
{{Form::label('date', 'Data')}}
</div>
<div class="col-md-8 col-sm-10 col-xs-10">
<div class="input-group">
<span class="input-group-addon" id="interval">Inicial</span>
{{Form::text('dateini', null, ['class' => 'form-control'])}}
<span class="input-group-addon" id="interval">Final</span>
{{Form::text('datefim', null, ['class' => 'form-control'])}}
</div>
</div>
</div>
How put the date in the format " d / m / Y " before the end of this check the controller?
如何在此检查控制器结束之前以“d / m / Y”格式放置日期?
public function Empenhos(Request $request)
{
$query = DB::table('empenho as emp')
->select('emp.nrEmpenho as a',DB::raw("DATE_FORMAT(emp.date, '%d/%m/%Y') as b"))
->orderby('emp.nrEmpenho');
if ($request->dateini) $query->where('emp.date', '>=', $request->dateini);
if ($request->datefim) $query->where('emp.date', '>=', $request->dateini)
->where('emp.date', '<=', $request->datefim);
$table = $query->paginate($request->perPage ? $request->perPage : 20);
$header = ['Numero', 'Data', 'Tipo', 'Credor', 'Ficha', 'Fonte', 'Valor'];
return view('results.planejamento.empenhos',
['perPage' => $request->perPage, 'title' => $this->title,
'title2' => $this->title2[6], 'header' => $header, 'table' => $table, 'return' => 'Empenhos']);
}
Ps: Any questions about the code I am available to provide any information necessary !
Ps:关于代码的任何问题我都可以提供任何必要的信息!
My english is very bad , sorry, i'm brazilian and i had to look for help here.
我的英语很糟糕,对不起,我是巴西人,我不得不在这里寻求帮助。
回答by Filip Koblański
Laravel's using Carbon\Carbon as an interface of Datetime, so You can use it by binding your input:
Laravel 使用 Carbon\Carbon 作为 Datetime 的接口,因此您可以通过绑定您的输入来使用它:
$dateinit = \Carbon\Carbon::parse($request->dateini);
$datefim = \Carbon\Carbon::parse($request->datefim);
$dateinit = \Carbon\Carbon::parse($request->dateini);
$datefim = \Carbon\Carbon::parse($request->datefim);
Now You can use Carbon object like this:
现在您可以像这样使用 Carbon 对象:
$dateinit->format('d/m/Y');
$dateinit->format('d/m/Y');
so the complete example code be like this:
所以完整的示例代码是这样的:
`
`
public function Empenhos(Request $request)
{
$dateinit = \Carbon\Carbon::parse($request->dateini);
$datefim = \Carbon\Carbon::parse($request->datefim);
$query = DB::table('empenho as emp')
->select('emp.nrEmpenho as a',DB::raw("DATE_FORMAT(emp.date, '%d/%m/%Y') as b"))
->orderby('emp.nrEmpenho');
if ($request->dateini) $query->where('emp.date', '>=', $dateinit->format('d/m/Y'));
if ($request->datefim) $query->where('emp.date', '>=', $dateinit->format('d/m/Y'))
->where('emp.date', '<=', $datefim->format('d/m/Y'));
$table = $query->paginate($request->perPage ? $request->perPage : 20);
$header = ['Numero', 'Data', 'Tipo', 'Credor', 'Ficha', 'Fonte', 'Valor'];
return view('results.planejamento.empenhos',
['perPage' => $request->perPage, 'title' => $this->title,
'title2' => $this->title2[6], 'header' => $header, 'table' => $table, 'return' => 'Empenhos']);
} `
}`
The only question is the default format of your Database timestamp and the date
field type.
唯一的问题是您的数据库时间戳和date
字段类型的默认格式。