我正在尝试显示在特定时间内输入的所有条目,从27-02-2018 22:00:00到28-02-2018 05:59:59,这是我到目前为止获得的结果,但我没有从网页中获得任何结果
$NightStart='22:00:00';
$NightEnd='05:59:59';
$dcmlogs = log::with('users')->whereBetween('created_at',['Carbon::today()->toDateString() $NightStart','Carbon::yesterday()->toDateString() $NightEnd'])->paginate(10);为什么我没有得到任何结果?我确信在数据库中有一些带有这些时间戳的行。
发布于 2018-02-28 18:55:55
Php不会只在"内部解析'中的值。
尝试:
$NightStart = '22:00:00';
$NightEnd = '05:59:59';
$today = Carbon::today()->toDateString() . ' ' . $NightStart;
$yesterday = Carbon::yesterday()->toDateString() . ' ' . $NightEnd;
$dcmlogs = log::with('users')->whereBetween('created_at',[$yesterday, $today])->paginate(10);发布于 2018-02-28 18:56:33
尝试更改此行:
$dcmlogs = log::with('users')->whereBetween('created_at',['Carbon::today()->toDateString() $NightStart','Carbon::yesterday()->toDateString() $NightEnd'])->paginate(10);至:
$fromTime = Carbon::yesterday()->toDateString()." $NightEnd";
$toTime = Carbon::today()->toDateString()." $NightStart";
$dcmlogs = log::with('users')
->whereBetween('created_at', [$fromTime, $toTime])
->paginate(10);发布于 2018-02-28 19:05:51
如果您希望完全使用碳完成此操作,您可以使用以下方法:
$dcmlogs = log::with('users')->whereBetween('created_at',[
Carbon::yesterday()->hour(5)->minute(59)->second(59)->toDateTimeString(),
Carbon::today()->hour(22)->toDateTimeString()
])->paginate(10);或者,您也可以使用setTime($h, $m, $s)来代替手动设置条件
$dcmlogs = log::with('users')->whereBetween('created_at',[
Carbon::yesterday()->setTime(5, 59, 59)->toDateTimeString(),
Carbon::today()->setTime(22, 0, 0)->toDateTimeString()
])->paginate(10);在您的示例中,您使用的是单引号,它将条件解析为字符串,并且没有正确地附加条件。
https://stackoverflow.com/questions/49027726
复制相似问题