Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

urhitech's avatar

How to get last week, last two weeks, and last three weeks data in Laravel

Hi! am developing an app to keep records of lotto results, and i want example last week results to be in green color, and last two weeks in yellow and last three weeks in red.

Am having challenges with the date and help please: This is my sample controller code.

    $now = Carbon::now();
    $start = $now->startOfWeek(Carbon::SATURDAY);
    $end = $now->endOfWeek(Carbon::MONDAY);

    $last_week = GameResult::where('game_id', $id)->whereBetween('date', [$start, $end])->first();
0 likes
2 replies
Snapey's avatar

your code is selecting to this week

Sinnbeck's avatar

Be sure to copy your carbon dates, as they are mutable

   $now = Carbon::now();
   $start = $now->copy()->startOfWeek(Carbon::SATURDAY);
   $end = $now->copy()->endOfWeek(Carbon::MONDAY);

Please or to participate in this conversation.