猿问

在laravel中获取当前小时内的数据

以下将带来从一天开始到一天结束的数据:

->whereBetween('created_at', [ Carbon::now()->startOfDay(),Carbon::now()->endOfDay()])

有没有办法将数据从当前小时的开始带到当前小时的结束?并且它是否也包括今天的日期,只是为了确保它不会在这一小时内而是在所有日子里获取数据?


拉风的咖菲猫
浏览 407回答 1
1回答

ITMISS

你现在只增加1小时怎么样?->whereBetween('created_at', [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;now()->format('Y-m-d H:00:00'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;now()->addHours(1)->format('Y-m-d H:00:00')]);例子:routes/web.php<?phpRoute::get('/', function () {&nbsp; &nbsp; $users = factory(App\User::class, 25)->create();&nbsp; &nbsp; foreach ($users->take(10) as $user) {&nbsp; &nbsp; &nbsp; &nbsp; $user->created_at = now()->addDay();&nbsp; &nbsp; &nbsp; &nbsp; $user->save();&nbsp; &nbsp; }&nbsp; &nbsp; return App\User::whereBetween('created_at', [&nbsp; &nbsp; &nbsp; &nbsp; now()->format('Y-m-d H:00:00'),&nbsp; &nbsp; &nbsp; &nbsp; now()->addHours(1)->format('Y-m-d H:00:00')&nbsp; &nbsp; ])->count();});&nbsp;结果:15仅返回今天这一小时内的用户
随时随地看视频慕课网APP
我要回答