Laravel: How to get count of all records created within in current week as of yesterday
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Laravel: Accurately Counting Records within the Current Week using created_at
As senior developers, we often deal with the complexities of date and time manipulation when querying databases. Calculating dynamic date ranges—like "the current week as of yesterday"—can quickly become a source of subtle bugs, especially when mixing PHP date functions with SQL date functions. The approach you presented attempts to solve this by calculating week boundaries in PHP and injecting them into raw SQL, but as you discovered, it often leads to inaccurate results due to timezone issues or incorrect boundary definitions.
This post will dive into why your initial query wasn't working reliably and provide the correct, robust, and idiomatic Laravel way to achieve accurate record counting within a specific time frame.
The Pitfall of Dynamic Date Calculation in Queries
The primary issue with dynamically building date strings like $name_current_week in PHP and using them directly in WHERE clauses is that it relies heavily on the server's local timezone settings and the specific implementation of MySQL/PostgreSQL date functions.
When you try to calculate week boundaries purely in PHP, you are dealing with abstract concepts (like "Monday this week") which don't always map perfectly to the precise timestamp stored in the created_at column when executed by the database engine. Relying on string comparisons for date ranges is fragile; using explicit timestamp boundaries is much safer and more performant.
The Correct Laravel Approach: Using Carbon for Time Boundaries
The most reliable way to handle time-based queries in a Laravel application is to leverage the powerful Carbon library, which is deeply integrated into Laravel. Carbon allows you to manage dates and times across different timezones effortlessly.
To get the count of records created within the last seven days (or a specific week window), we should define the exact start and end timestamps using Carbon's relative date methods.
Here is how you can correctly calculate the required range—records created between yesterday and seven days ago, or within the current calendar week boundaries—using Eloquent:
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
// Define the boundaries using Carbon
$yesterday = Carbon::now()->subDay();
$oneWeekAgo = Carbon::now()->subWeek();
// Example of counting records created within the last 7 days (a common requirement)
$count = YourModel::where('created_at', '>=', $oneWeekAgo)
->where('created_at', '<=', $yesterday)
->count();
// Or, if you specifically want all records from the start of the current week up to yesterday:
$startOfWeek = Carbon::now()->startOfWeek();
$endOfDayYesterday = $yesterday->endOfDay();
$weeklyCount = YourModel::where('created_at', '>=', $startOfWeek)
->where('created_at', '<=', $endOfDayYesterday)
->count();
Refined Raw Query Approach (If Necessary)
If you absolutely need to perform this calculation in a highly optimized raw SQL query, it is better to let the database handle the date arithmetic entirely. For example, to count records created since the beginning of the current week:
use Illuminate\Support\Facades\DB;
$startDate = Carbon::now()->startOfWeek()->toDateString();
$endDate = Carbon::now()->subDay()->toDateString(); // Yesterday
$count = DB::table('your_table')
->whereDate('created_at', '>=', $startDate)
->whereDate('created_at', '<=', $endDate)
->count();
Notice how this approach delegates the complex date math to the database engine, which is much more reliable than relying on string manipulation in PHP. This principle of letting the database handle time logic is fundamental when building scalable applications with Laravel. For further insights into efficient Eloquent usage and database interactions, check out the official documentation at https://laravelcompany.com.
Conclusion
The issue you faced stems from trying to force complex temporal logic using string concatenation in PHP rather than leveraging the powerful date objects provided by Carbon or letting the database manage the comparison. Always prioritize using explicit timestamp comparisons (>= and <=) with precise Carbon instances when querying time-sensitive data in Laravel. This ensures your results are accurate, timezone-aware, and maintainable, which is crucial for any production-grade application you build.