How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Get Current Month Records in Laravel Eloquent
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Learn > Web Development > How to Get Current Month Records in Laravel Eloquent
Web Development

How to Get Current Month Records in Laravel Eloquent

how7o
By how7o
Last updated: April 20, 2026
8 Min Read
Laravel Eloquent current month records — calendar and query builder illustration
SHARE

When you need laravel eloquent current month records — posts, orders, or any timestamped row created in the ongoing calendar month — Laravel gives you three idiomatic options: whereMonth + whereYear, a whereBetween range using Carbon’s startOfMonth() / endOfMonth(), or a Carbon-driven pair using Carbon::now()->month. This guide shows each approach, explains which one actually uses your database index, and covers the timezone gotcha that trips up most developers near midnight.

Contents
  • TL;DR
  • Option 1 — whereMonth + whereYear with PHP date()
  • Option 2 — Carbon-based month filtering
  • Option 3 — whereBetween with startOfMonth / endOfMonth (recommended)
  • Performance: why the range form wins on big tables
  • The timezone gotcha
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-21 on Laravel 11 with PHP 8.3 and MySQL 8.0. Originally published 2022-10-08, rewritten and updated 2026-04-21.

TL;DR

For a quick fix use Post::whereMonth('created_at', date('m'))->whereYear('created_at', date('Y'))->get(). For production and large tables, prefer the index-friendly range form: Post::whereBetween('created_at', [now()->startOfMonth(), now()->endOfMonth()])->get(). Always match your app timezone to the timezone of the stored created_at values.

Option 1 — whereMonth + whereYear with PHP date()

The shortest way to filter rows created this month is to combine Eloquent’s whereMonth and whereYear scopes with PHP’s built-in date() function. No Carbon import required:

$posts = Post::whereMonth('created_at', date('m'))
    ->whereYear('created_at', date('Y'))->get();

date('m') returns the current month as a two-digit number (01–12) and date('Y') returns the four-digit year. Eloquent translates the scopes into MONTH(created_at) = ? AND YEAR(created_at) = ? in SQL.

This works on every Laravel version back to 5.x, but there’s a performance catch we’ll return to in a moment.

Option 2 — Carbon-based month filtering

If you already import Carbon (use Carbon\Carbon; or via Laravel’s now() helper), you can drop date() in favor of Carbon’s property accessors:

use Carbon\Carbon;

$posts = Post::whereMonth('created_at', Carbon::now()->month)
    ->whereYear('created_at', Carbon::now()->year)
    ->get();

Functionally identical to Option 1 — Carbon’s ->month and ->year just wrap PHP’s date() internally. The advantage is that Carbon makes timezone handling explicit: Carbon::now('UTC')->month evaluates “month” in UTC regardless of your app timezone.

laravel eloquent current month records — whereMonth vs whereBetween index comparison

Option 3 — whereBetween with startOfMonth / endOfMonth (recommended)

This is the form I reach for in production. It uses a timestamp range instead of month-year arithmetic, which means the database can answer the query with a B-tree index range scan on created_at:

use Carbon\Carbon;

$start = Carbon::now()->startOfMonth();
$end   = Carbon::now()->endOfMonth();

$posts = Post::whereBetween('created_at', [$start, $end])->get();

Or, with the now() helper for a one-liner:

$posts = Post::whereBetween('created_at', [
    now()->startOfMonth(),
    now()->endOfMonth(),
])->get();

startOfMonth() returns YYYY-MM-01 00:00:00 and endOfMonth() returns the last day at 23:59:59, so the between is inclusive at both edges. This form also sidesteps the “year rollover” issue where whereMonth(1) in January would return January rows from every previous year as well — whereYear is required with Options 1 and 2, but is redundant here because the range itself is anchored to a specific year.

Performance: why the range form wins on big tables

Run EXPLAIN on each query and you’ll see the difference. whereMonth('created_at', ?) compiles to MONTH(created_at) = ?. MySQL can’t use a normal index on created_at once the column is wrapped in a function — it falls back to a full table scan or forces you to maintain a functional index.

whereBetween('created_at', [$start, $end]) compiles to created_at BETWEEN ? AND ?, which the optimizer resolves as a range scan on any B-tree index covering created_at. On tables over ~100k rows the difference between these two plans is typically one or two orders of magnitude in query time.

If created_at doesn’t already have an index, add one in a migration — see adding columns to an existing table for the pattern and use $table->index('created_at').

The timezone gotcha

All three options rely on “the current month” being the same month on both sides of the wire: in PHP (your app timezone, set in config/app.php) and in the database (where created_at is stored, usually UTC). When those disagree, you get ghosts near midnight.

Concrete example: your app’s timezone config is 'Asia/Dhaka' (UTC+6), but MySQL stores timestamps in UTC. A row inserted at 2026-05-01 02:00 Dhaka time is stored as 2026-04-30 20:00 UTC. On the first of the month in Dhaka, that row already “belongs to May” from the app’s perspective, but whereMonth('created_at', 5) against the UTC timestamp won’t match it.

Two clean fixes: either set config/app.php 'timezone' => 'UTC' and convert for display only, or pass a timezone-aware Carbon into the range — Carbon::now('Asia/Dhaka')->startOfMonth()->setTimezone('UTC') — so the window boundaries match the stored column’s zone.

Frequently asked questions

How do I get laravel eloquent current month records without Carbon?

Use PHP’s built-in date() function with whereMonth and whereYear: Post::whereMonth('created_at', date('m'))->whereYear('created_at', date('Y'))->get(). This works on any Laravel version and avoids pulling in Carbon if you haven’t imported it in that file.

What’s the most idiomatic modern approach in Laravel 11?

Use whereBetween with Carbon’s startOfMonth() and endOfMonth() helpers: Post::whereBetween('created_at', [Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth()])->get(). It generates a single range query that the database can resolve with an index scan, unlike whereMonth/whereYear which apply functions to the column.

Why does my query return the wrong month near midnight?

Timezone mismatch. date('m') uses PHP’s configured timezone (config/app.php or the server default), while created_at in MySQL is typically stored as UTC. If your app timezone is Asia/Dhaka but the DB is UTC, rows inserted between 18:00 and 23:59 UTC will appear to be “next month” from PHP’s perspective. Either align both timezones or convert explicitly with Carbon::now('UTC').

Will whereMonth use an index on created_at?

No. whereMonth compiles to MONTH(created_at) = ?, which wraps the column in a function and prevents MySQL from using a B-tree index on created_at. For large tables, prefer the whereBetween range approach — it produces created_at BETWEEN ? AND ?, which the optimizer can answer with an index range scan.

Can I get current month records grouped by day?

Yes — chain selectRaw with a DATE() expression and groupBy: Post::whereBetween('created_at', [now()->startOfMonth(), now()->endOfMonth()])->selectRaw('DATE(created_at) as day, COUNT(*) as total')->groupBy('day')->get(). Useful for building a daily-activity chart for the current month.

Related guides

  • How to Install Laravel on Ubuntu — get your Laravel 11 environment ready first.
  • How to Get Records Created Today in Laravel — the single-day variant of this query.
  • How to Count Rows in Laravel Eloquent — pair month filtering with a count for dashboard widgets.
  • Laravel Eloquent group by + count — group current-month rows by day or category.

References

Official Laravel query builder docs (where/whereBetween/whereMonth): laravel.com/docs/queries.

TAGGED:EloquentLaravelmysqlphp

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Laravel Eloquent group by count — Book::groupBy('author') query with bar-chart aggregate icon How to Count Records Grouped By a Column in Laravel Eloquent
Next Article Laravel Eloquent records today — Carbon today helper and whereDate illustration How to Get Records Created Today in Laravel
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Display PHP errors — ini_set + php.ini configuration
How to Display PHP Errors
May 10, 2026
PHP convert string to uppercase — strtoupper and mb_strtoupper
How to Convert a String to Uppercase in PHP
May 10, 2026
PHP string to float conversion with cast, regex cleanup, NumberFormatter
How to Convert a String to Float in PHP
May 10, 2026
PHP merge arrays without duplicates — union operator and array_unique
How to Combine Two Arrays Without Duplicates in PHP
May 10, 2026
PHP delete array element — unset, array_splice, array_filter, array_search
How to Delete an Element from a PHP Array
May 10, 2026

You Might Also Like

CyberPanel too many redirects Laravel error fixed by restarting LiteSpeed
Server Management

Fix CyberPanel Too Many Redirects (ERR_TOO_MANY_REDIRECTS) for Laravel Subdomain

7 Min Read
WooCommerce add custom fee — woocommerce_cart_calculate_fees + WC()->cart->add_fee
Web Development

How to Add a Custom Fee (or Transaction Fee) in WooCommerce

8 Min Read
Install Composer on Ubuntu — terminal with composer-setup.php and PHP elephant icon
Web Development

How to Install Composer on Ubuntu: Step-by-Step Guide

8 Min Read
MariaDB restart on Linux — systemctl restart mariadb
Server Management

How to Restart the MariaDB Server on Linux

6 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?