碳和Eloquent次

Carbon and Eloquent times

我正在尝试显示在特定时间内输入的所有条目,从 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);

为什么我没有得到任何结果?我确定数据库中有一些带有这些时间戳的行。

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);

尝试更改此行:

$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);

如果您希望完全使用 Carbon 来完成此操作,您可以使用以下方法:

$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);

您的示例使用单引号将您的条件解析为字符串,但也没有正确附加。