【问题标题】:How to fetch all the records between date in laravel?如何在laravel中获取日期之间的所有记录?
【发布时间】:2019-02-16 18:01:16
【问题描述】:

我正在获取此错误:

DateTime::__construct():在位置 7 (3) 解析时间字符串 (11-08-33) 失败:意外字符”

 $fromDate = Carbon::parse($request->input('start'))->format('Y-m-d');
$toDate = Carbon::parse($request->input('end'))->format('Y-m-d');
$date_range = [$fromDate . ' 00:00:00', $toDate . ' 23:59:59'];

$data = DB::where('projects')
->whereBetween('created_at', $date_range)
->get();

【问题讨论】:

  • 你缺少 table(), DB::table('projects')

标签: laravel-5


【解决方案1】:

你必须指定Carbon unput 字符串格式,所以,在这种情况下你可以改变你的代码如下:

$input_date_format="d-m-y";//your date input format

$fromDate = Carbon::createFromFormat($input_date_format,$request->input('start'))->format('Y-m-d 00:00:00');
$toDate = Carbon::createFromFormat($input_date_format,$request->input('end'))->format('Y-m-d 23:59:59');


//instead of `DB::where`, use `DB::table`
$data = DB::table('projects')->whereBetween('created_at',[$fromDate, $toDate ])->get();

注意:您也可以使用str_replace- 替换为/,这样您的代码就可以工作了!

【讨论】:

  • 感谢 Soheli Rahmat。但现在我收到“尾随数据”错误。
  • \Datetime::createFromFormat($your_format,$input_data)->format($your_requested_format)测试!
猜你喜欢
  • 2016-08-04
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 2017-03-27
  • 1970-01-01
  • 2019-03-14
  • 1970-01-01
  • 2013-07-07
相关资源
最近更新 更多