碳和雄辩时代

时间:2018-02-28 10:48:35

标签: php laravel eloquent php-carbon

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

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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

尝试更改此行:

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

答案 2 :(得分:0)

如果您希望完全使用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);

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