php Laravel -获取过去24小时、1周或1个月的数据

kyxcudwk  于 2023-02-03  发布在  PHP
关注(0)|答案(3)|浏览(410)

因此,我尝试只获取过去24小时、1周、1个月的数据。我有工作代码,但如果我正确获取,则为“其他方式”。我的意思是,如果日期为2016年11月30日,并且我将数据值设置为2016年12月1日,则24小时仍然获取信息,但它不应该。如果日期是2016年11月30日,而我将其设置为2016年11月28日,则24小时服务器无法获取它。对我来说,这听起来像是倒退。我希望解释是可以理解的。

$getItemsOneDay = Deposit::where('steam_user_id',0)->where('status', Deposit::STATUS_ACTIVE)->where('created_at', '>', Carbon::now()->subMinutes(1440))->get();
$getItemsOneWeek = Deposit::where('steam_user_id',0)->where('status', Deposit::STATUS_ACTIVE)->where('created_at', '>', Carbon::now()->subMinutes(10080))->get();
$getItemsOneMonth = Deposit::where('steam_user_id',0)->where('status', Deposit::STATUS_ACTIVE)->where('created_at', '>', Carbon::now()->subMinutes(43200))->get();
uajslkp6

uajslkp61#

你写的是created_at > time
所以你让laravel搜索所有大于现在的数据(),所以你展望未来。
要么你想...

  • 查看某个时间点之前的点:created_at < time
  • 查看某个时间点之后的点:created_at > time
  • 查看过去时间间隔内的点:created_at > start && created_at < end

如果我没猜错的话,您正在搜索第三个选项,因此您需要执行类似...->where("created_at",">",Carbon::now()->subDay())->where("created_at","<",Carbon::now())->...的操作
希望你在这里。

kuarbcqp

kuarbcqp2#

->where('created_at', '>=', Carbon::now()->subDay()->toDateTimeString())->get();
获取最近24小时内创建的数据

ldxq2e6h

ldxq2e6h3#

检查以下功能;它工作得很好。

public function getanswer_24hours(Request $request)
      {
          $ans = Answer::where('user_id',$request->user_id)->where('created_at', '>=', Carbon::now()->subDay()->toDateTimeString())->get();
          
          if(count($ans) > 0)
        {
           
            $result = 'yes';
            return response()->json(['data' =>['answer' => $ans,'result' => $result, 'message' => 'get Answer Before 24hours SuccessFully!!','status' => true],200,'ok']);
            
        }else{
           
            $result = 'no';
            return response()->json(['data' =>['answer' => [], 'result' => $result, 'message' => 'get Answer Failed!!'],404,'no']);
        }

          
      }

相关问题