How to get data of previous week in Laravel 5.1?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Date Ranges: How to Get Data for the Previous Week in Laravel
As developers, one of the most common tasks we face is manipulating time-series data—filtering records based on specific calendar periods like weeks, months, or years. When working with databases and frameworks like Laravel, handling these date boundaries correctly can often be tricky, especially when dealing with custom week definitions (like Sunday start vs. Monday start).
Today, we are diving into a common challenge: retrieving all data inserted during the previous full calendar week in a Laravel application running on an older framework version, specifically focusing on how to fix an existing date query.
The Problem with Simple Subtraction
You are currently attempting to use Carbon to calculate the previous week's range using simple subtraction:
$AgoDate = Carbon::now()->subWeek()->format('Y-m-d');
$NowDate = Carbon::now()->format('Y-m-d');
// This query returns the last 7 days
$query->whereBetween('created_on', array($AgoDate, $NowDate));
As you correctly noted, this method only retrieves the last seven days. It is excellent for fetching recent activity but fails when you need a fixed calendar week (e.g., January 31st to February 6th), which represents the previous full week, regardless of where today falls within that span.
The issue lies in how subWeek() operates; it subtracts exactly seven days from the current moment, resulting in a rolling 7-day window rather than a defined weekly boundary. To achieve true "previous week" retrieval, we need to explicitly calculate the start and end dates of that specific week.
The Developer Solution: Calculating Week Boundaries
To accurately capture the previous full week, we must anchor our calculations to the start day of the week (in this case, Sunday). We can leverage Carbon’s powerful methods to determine these boundaries precisely.
Since your requirement specifies that the week starts on Sunday, we need a method to find the Sunday preceding the current date and the Saturday following it.
Here is the robust approach using Carbon:
use Carbon\Carbon;
// 1. Get the current date
$now = Carbon::now();
// 2. Determine the start of the current week (Sunday)
// Carbon::setWeekStartsAt(0) ensures Sunday is treated as the start of the week (0=Sunday, 1=Monday, etc.)
$currentWeekStart = $now->startOfWeek(Carbon::SUNDAY);
// 3. Calculate the start of the previous week (7 days before the current week's start)
$previousWeekStart = $currentWeekStart->copy()->subWeek(); // Subtracting 1 week moves us to the previous Sunday
// 4. Calculate the end of the previous week (the Saturday following the previous start date)
$previousWeekEnd = $previousWeekStart->copy()->addDays(6); // Add 6 days to get the preceding Saturday
// Now use these bounds in your query
$startDate = $previousWeekStart->toDateString();
$endDate = $previousWeekEnd->toDateString();
$query->whereBetween('created_on', array($startDate, $endDate));
Breakdown of the Logic
startOfWeek(Carbon::SUNDAY): This method is crucial. It correctly identifies the start of the week based on your custom rule (Sunday).- Subsequent Subtraction/Addition: By starting from the current week's start and subtracting a full week (
subWeek()), we land directly on the Sunday of the previous week. Adding 6 days to that result gives us the Saturday, defining the precise boundary for the entire previous week.
This method ensures that your query always captures exactly seven days of data corresponding to the calendar week immediately preceding the current date, solving the ambiguity of simple date subtraction. This level of precision is fundamental when building complex reporting features in Laravel, often utilizing robust Eloquent relationships to manage this kind of temporal logic effectively, as promoted by the principles found on https://laravelcompany.com.
Conclusion
Moving beyond simple date math and embracing the contextual awareness provided by libraries like Carbon is essential for writing reliable backend code. By explicitly calculating the start and end dates of the desired calendar week—rather than relying on relative offsets—you gain control over your data retrieval, ensuring that your application delivers exactly the data intended. Use these precise calculations to manage time-series queries consistently across all your Laravel applications.