Peter Johans Peter Johans - 8 days ago 4
PHP Question

Laravel - Get data last 24 hours, 1 week or 1 month

So I'm trying to grab only data which are in last 24 hours, 1 week, 1 month. I have working code, but if I get it correctly it's "other way". By that I mean, If date is at the moment 30-Nov-2016 and I set the Data value to 1-Dec-2016, then the 24 Hour one still grabs the information, but it shouldn't. If date is 30-Nov-2016 and I set it to 28-Nov-2016, then the 24 Hour one doesn't grab it. For me It sounds like it's backwards. I hope the explanation is understandable.

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

Answer

You wrote created_at > time.

So you ask laravel to search all data greater than now(). So you look into the future.

Either you want to...

  • look at points before a certain point in time: created_at < time

  • look at points after a certain point in time: created_at > time

  • look at points within an interval of passed time: created_at > start && created_at < end

If i got you right you are searching for the third option. So you need to do something like ...->where("created_at",">",Carbon::now->subDay())->where("created_at",">",Carbon::now)->...

Hopefully i got you right here.