我将视图中的fromdate
和todate
传递给控制器。我想从数据库中打印这些日期,但我得到的错误如下:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError(E_ERROR) 在字符串
上调用成员函数format()
我的观看页面:
<div class="col-sm-4">
{!! Form::open (['url' => 'admin/ad']) !!}
<div class="form-group">
{!! Form::label('', 'From date') !!}
{!! Form::date('fromdate', '', ['class' => 'form-control']) !!}
{!! Form::label('', 'To date') !!}
{!! Form::date('todate', '', ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit(('submit'), ['class' => 'btn btn-primary btn-block']) !!}
{!! Form::close() !!}
</div>
</div>
我的控制器页面:
$report = $request->report;
$fromdate = $request->fromdate;
$start_date = date("Y-m-d H:i:s", strtotime($fromdate));
$todate = $request->todate;
$end_date = date("Y-m-d H:i:s", strtotime($todate));
$users = Report::whereBetween('created_at', [
$start_date->format('Y-m-d') . " 00:00:00",
$end_date->format('Y-m-d') . " 23:59:59"
])->get();
答案 0 :(得分:2)
使用Carbon库来处理日期解析和格式化:
$users = Report::whereBetween('created_at', [
Carbon::parse($request->fromdate)->toDateTimeString(),
Carbon::parse($request->todate)->toDateTimeString()
])->get();
请参阅 Modifiers 部分了解startOfXXXX
格式:
$users = Report::whereBetween('created_at', [
Carbon::parse($request->fromdate)->startOfDay(), // 2012-01-31 00:00:00
Carbon::parse($request->todate)->endOfDay() // 2012-01-31 23:59:59
])->get();
答案 1 :(得分:0)
好的,我使用
{{ Carbon\Carbon::parse($object->yourdate_fromdatabase)->format('Y-m-d') }}
答案 2 :(得分:0)
放置在模型中
protected $dates = ['fromdate', 'todate'];
答案 3 :(得分:-1)
您还可以使用DateTime
$report = $request->report;
$fromdate = $request->fromdate;
$start_date = DateTime::createFromFormat("U", strtotime($fromdate));
$todate = $request->todate;
$end_date = DateTime::createFromFormat("U", strtotime($todate));
$users = Report::whereBetween('created_at', [
$start_date->format('Y-m-d') . " 00:00:00",
$end_date->format('Y-m-d') . " 23:59:59"
])->get();
答案 4 :(得分:-1)
$report = $request->report;
$fromdate = $request->fromdate;
$start_date = date("Y-m-d H:i:s", strtotime($fromdate));
$todate = $request->todate;
$end_date = date("Y-m-d H:i:s", strtotime($todate));
$users = Report::whereBetween('created_at', [$start_date,$end_date])->get();