how to check current date is between two dates in laravel 5.4

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Check Current Date Between Two Dates in Laravel

Dealing with date range comparisons is a fundamental task in application development, whether you are querying a database directly or building an API layer using a framework like Laravel. When you need to determine if the current date falls within a specific period defined by two columns in your table, it requires careful handling of date types, time zones, and the correct use of Eloquent or the Query Builder methods.

This post will walk you through the common pitfalls encountered when trying to perform this check in Laravel, starting from your initial SQL attempt to finding the robust, idiomatic solution.

The Scenario: Date Range Filtering

Let's start with the scenario you presented. You have a table named Financial_Year structured like this:

    +---------------------+-----------+
    | id | start_date     |  End_date |
    +---------------------+-----------+
|   1  | 2017-07-07   | 2017-07-31|
|   2  | 2017-08-01   | 2017-08-31|
|   3  | 2017-09-01   | 2017-09-10|
+---------------------+-----------+

You want to select records where the current date (CURDATE()) falls inclusively between the start_date and End_date columns for that specific row.

Your initial attempt involved trying to translate this directly into a Laravel query:

SELECT * FROM Financial_Year WHERE CURDATE() between `start_date` and `End_date`

And then attempting the Laravel equivalent:

$dt = Carbon::now();
$getmonths = DB::table('Financial_Year')
               ->whereBetween($dt, ['start_date', 'End_date'])
               ->get(); // This resulted in no output.

Why the Initial Attempt Failed

The reason your initial Laravel attempt failed is twofold:

  1. Mixing Current Date with Column Ranges: The whereBetween() method expects you to provide a single value (or an array of values) to compare against the specified column(s). When you use $dt (the current date) as the first parameter, and then pass an array of columns (['start_date', 'End_date']) as the second parameter, Laravel doesn't interpret this as a row-by-row comparison correctly in the context of whereBetween.
  2. Date Type Mismatch: When dealing with date ranges for filtering records, you generally don't compare a single point in time (Carbon::now()) against two separate range columns simultaneously using whereBetween in this manner.

The Correct Laravel Solution: Comparing Today Directly

The most efficient and idiomatic way to solve this is to use standard comparison operators directly within the where clause, utilizing the database's ability to handle date functions like CURDATE() or direct string/date comparisons.

Since you are checking if a specific day exists within a range defined by the row, you should compare the current date against both boundaries explicitly.

Solution using the Query Builder

For maximum compatibility and performance in Laravel, especially when dealing with raw database functions, use the where clause directly:

use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

// 1. Get the current date for comparison (as a string or Carbon instance)
$today = Carbon::now()->toDateString(); // Use toDateString() for clean YYYY-MM-DD comparison

$results = DB::table('Financial_Year')
             ->where(function ($query) use ($today) {
                 // Check if today is greater than or equal to the start date
                 $query->where('start_date', '<=', $today)
                       // AND check if today is less than or equal to the end date
                       ->where('End_date', '>=', $today);
             })
             ->get();

// Or, using a slightly more direct comparison structure:
$results_direct = DB::table('Financial_Year')
                 ->where(function ($query) {
                     $query->whereDate('start_date', '>=', Carbon::now()->startOfDay())
                           ->whereDate('End_date', '<=', Carbon::now()->endOfDay());
                 })
                 ->get();

// Output the results
// dd($results);

Explanation of Best Practices

  1. Use whereDate() for Date-Only Comparisons: When you only care about the date component (ignoring time), using whereDate('column', 'operator', 'value') is much cleaner than relying on raw functions like CURDATE() inside the query string, as it keeps the logic within the Eloquent/Query Builder structure.
  2. Using Carbon for Context: We use Carbon::now()->startOfDay() and endOfDay() to ensure we are comparing dates accurately without worrying about potential time zone shifts or time components confusing the comparison.
  3. Clarity over Complexity: While raw SQL (WHERE CURDATE() BETWEEN start_date AND End_date) is concise, wrapping this logic within a closure using where clauses provides better abstraction and security, which is a core principle of building robust applications with Laravel. As we strive for clean code, leveraging the power of the framework is key, much like when structuring complex data access patterns on https://laravelcompany.com.

Conclusion

Checking if the current date falls within a stored date range requires moving away from attempting to force raw SQL constructs into generic methods like whereBetween(). Instead, leverage Laravel's robust Query Builder features—specifically the where clause paired with Carbon objects—to perform explicit, readable, and timezone-aware comparisons. By focusing on what you need (a comparison between two boundaries) rather than a single range check, you ensure your code is both secure and highly maintainable.