在 Laravel 的字符串上调用成员函数 format()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50020692/
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
Call to a member function format() on string in laravel
提问by robert
I am passing the fromdate
and todate
from the view to controller. I want to print the betweens these dates from the database, but I get error like:
我正在将fromdate
和todate
从视图传递给控制器。我想从数据库中打印这些日期之间的时间,但出现如下错误:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Call to a member function format() on string
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Call to a member function format() on string
my view page:
我的查看页面:
<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>
my controller page:
我的控制器页面:
$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();
采纳答案by Gopi Chand
$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();
回答by DigitalDrifter
Use the Carbonlibrary to handle date parsing and formatting:
使用Carbon库处理日期解析和格式化:
$users = Report::whereBetween('created_at', [
Carbon::parse($request->fromdate)->toDateTimeString(),
Carbon::parse($request->todate)->toDateTimeString()
])->get();
See the Modifierssection for startOfXXXX
formatting:
有关格式,请参阅修饰符部分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();
回答by jay dave
ok you me use
好的你我用
{{ Carbon\Carbon::parse($object->yourdate_fromdatabase)->format('Y-m-d') }}
回答by Chetam Okafor
place in your model:
放置在您的模型中:
protected $dates = ['fromdate', 'todate'];
回答by linktoahref
You could also make use of DateTime
您还可以使用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();