How to Get Data Between Two Dates in Laravel 12

Learn how to get data between two dates in Laravel 12 using WhereBetween, WhereDate, Where and compare datetime values with same date.

You can easily retrieve data between two dates in Laravel 12 ,laravel 11, laravel 10, laravel 9, laravel 8, laravel 7, laravel 6, laravel 5.

We’ll cover four different examples to get data between two dates. These examples will use the created_at column to fetch data between two dates.

Example 1: Using whereBetween() Condition

The whereBetween method retrieves records where the given column’s value is greater than or equal to the first datetime and less than or equal to the second datetime.

It accepts two parameters:

  • The first is the column name.
  • The second is an array containing two values to compare.
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;
use Carbon\Carbon;

class DemoController extends Controller
{
    public function getDate(Request $request)
    {  
         $startDate = Carbon::createFromFormat('d/m/Y', '03/07/2025')->startOfDay();
         $endDate = Carbon::createFromFormat('d/m/Y', '07/07/2025')->endOfDay();       
         $posts =   Post::select('title', 'created_at')->whereBetween('created_at', [$startDate, $endDate])->get()->toArray();
         dd($posts);
    }
}

⚠️ Issue: If you are using whereBetween() like this:

 $startDate = Carbon::createFromFormat('d/m/Y', '03/07/2025');
 $endDate = Carbon::createFromFormat('d/m/Y', '07/07/2025');
 $posts =   Post::select('title', 'created_at')->whereBetween('created_at', [$startDate, $endDate])->get()->toArray();
 dd($plans);

The issue is that the created_at column stores a full datetime, not just a date. So when you filter using a date like 2025-07-07 , SQL treats it 2025-07-07 14:35:00(current time). As a result, any records created later on 7 July after 14:35:00 will not be retrieved.

When you apply whereBetween() to a datetime column like created_at, make sure your values include both start and end of day, otherwise you might miss records. startOfDay() and endOfDay() ensures the query retrieve all records created any time between 2025‑07‑05 00:00:00 and 2025‑07‑07 23:59:59, inclusive.

Read Also: Laravel 12 How to Change Date Format Example

Example 2: Using WhereDate() Method

The whereDate method is used to compare a column’s value against a specific date. It automatically converts the column’s datetime value to the Y-m-d format. This allows you to match records based solely on the date, ensuring accurate results.

The whereDate method accepts

  • The first is the column name.
  • The second is the comparison operator (such as =, <, or >).
  • The third is the date value to compare against.

If you want to check whether a column is equal to a specific date, you can pass the date directly as the second argument to the whereDate method—without needing to include operators like =, <, or >.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;
use Carbon\Carbon;

class DemoController extends Controller
{
    public function getDate(Request $request)
    {  
         $startDate = Carbon::createFromFormat('d/m/Y', '03/07/2025');
         $endDate = Carbon::createFromFormat('d/m/Y', '07/07/2025');      
         $posts =   Post:: select('title', 'created_at')->whereDate('created_at', '>=', $startDate)->whereDate('created_at', '<=', $endDate)->get()->toArray();
         dd($posts);
    }
}

Read Also : Laravel 12 Toastr JS Notifications Example

Example 3: Using Where condition

The where method works similarly to whereBetween when filtering by date ranges. However, to get accurate results with datetime columns, you should use startOfDay() and endOfDay() to cover the full range of the selected dates.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;
use Carbon\Carbon;

class DemoController extends Controller
{
    public function getDate(Request $request)
    {  
         $startDate = Carbon::createFromFormat('d/m/Y', '03/07/2025')->startOfDay();
         $endDate = Carbon::createFromFormat('d/m/Y', '07/07/2025')->endOfDay();      
         $posts =   Post:: select('title', 'created_at')->where('created_at', '>=', $startDate)->where('created_at', '<=', $endDate)->get()->toArray();
         dd($posts);
    }
}

Example 4: Compare Datetime Values With Same Date

You can use various methods to compare datetime values that uses the same date, such as where or whereBetween. In this example, I’m using whereBetween for clarity and efficiency.

When comparing records for the same date, it’s important to include the start and end timestamps of the day to ensure all data is accurately retrieved. Whether you’re using whereBetween or individual where conditions, functions like startOfDay() and endOfDay() should be applied to capture the full date range.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;
use Carbon\Carbon;

class DemoController extends Controller
{
    public function getDate(Request $request)
    {  
         $startDate = Carbon::createFromFormat('d/m/Y', '07/07/2025')->startOfDay();
         $endDate = Carbon::createFromFormat('d/m/Y', '07/07/2025')->endOfDay();       
         $posts =   Post::select('title', 'created_at')->whereBetween('created_at', [$startDate, $endDate])->get()->toArray();
         dd($posts);
    }
}

Conclusion

Getting data between two dates in Laravel 12 is simple and flexible. You can use whereBetween(), whereDate(), or regular where conditions depending on whether you need full datetime accuracy or just date-only comparisons.

The key point is that columns like created_at store both date and time, so you should use startOfDay() and endOfDay() to make sure you capture the entire range of records.

With these approaches, you can reliably fetch records between two dates in Laravel — and the same methods work in older versions like Laravel 11, 10, 9, 8, 7, 6, and even 5.


Frequently Asked Questions (FAQs)

Q1: How do I get records created between two timestamps?

Use whereBetween on a datetime column: Model::whereBetween(‘created_at’, [$start, $end])->get(); supply Carbon instances for $start and $end.

Q2: What’s the difference between whereBetween and whereDate?

whereBetween compares full datetime values and is ideal when time matters, while whereDate truncates to Y-m-d and is best for day-level comparisons without time-of-day effects.

Q3: How can I include the full end day in my range?

Wrap with day boundaries: $start->startOfDay() and $end->endOfDay(), or use whereDate(‘created_at’, ‘>=’, $startDate) and whereDate(‘created_at’, ‘<=’, $endDate).

Q4: Why did my range return empty results using startOfWeek/endOfWeek ?

When you use Carbon, methods like startOfWeek() or endOfWeek() change the original date object instead of creating a new one.

So if you do this:

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

⚠️ Problem: After calling startOfWeek(), the $range object is already changed. Then when you call endOfWeek(), it overwrites it again. In the end, both $start and $end point to the same final date, so your range is collapsed (empty).

Solution: Use two separate variables, or clone/copy the date before changing it:

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

Q5: Can I filter by a custom input format like d/m/Y?

Yes, parse with Carbon::createFromFormat and then apply whereBetween or whereDate with the resulting Carbon objects

Q6: How do I filter by “current month” or “last 30 days”?

For rolling windows, use Carbon ranges such as [Carbon::now()->subDays(30), Carbon::now()] with whereBetween; for calendar months, use startOfMonth/endOfMonth.

Q7: How do I handle inclusive vs. exclusive range edges?

Laravel’s whereBetween() is inclusive (>= start and <= end).

  • Use startOfDay() / endOfDay() to cover full days.
  • For exclusive ranges, use < or > with where instead of whereBetween().

Inclusive = both edges counted, Exclusive = one edge skipped.

Q8: What if I need to reuse this logic across models?

Create a reusable local scope, e.g., scopeCreatedBetweenDates($query, [$start, $end]) using whereDate or whereBetween, and call Model::createdBetweenDates([$start, $end])->get().

I hope it helps..