碳和雄辩的时代

问题描述 投票:0回答:3

我想显示在特定时间内输入的所有条目,从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 laravel eloquent php-carbon
3个回答
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);

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.