How to select all rows for a certain month (or day, year or time), using Eloquent

Laravel: How to select all rows for a certain month (or day, year or time), using Eloquent

Eloquent makes many things easy, and working with dates is one of them!

There are bunch of built in functions for building a query that work with dates and times:

  • whereDate
  • whereMonth
  • whereDay
  • whereYear
  • whereTime

You can use them like this:

You can use the whereDate() method to compare a column's value against a date:

$users = DB::table('users')
        ->whereDate('created_at', '2019-11-31')
        ->get();

The whereMonth() method can be used for comparing a column's value against a specific month of a year:

$users = DB::table('users')
        ->whereMonth('created_at', '10')
        ->get();

The whereDay() method may be used to compare a column's value against a specific day of a month:

$users = DB::table('users')
        ->whereDay('created_at', '20')
        ->get();

The whereYear() method may be used to compare a column's value against a specific year:

$users = DB::table('users')
        ->whereYear('created_at', '2019')
        ->get();

The whereTime() method may be used to compare a column's value against a specific time:

$users = DB::table('users')
        ->whereTime('created_at', '=', '11:20:45')
        ->get();
webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to select all rows for a certain month (or day, year or time), using Eloquent

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the Laravel language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to add the default charset to UTF-8 in .htaccess

How to use subdomains as parameters in your routes (or in your web.php file)

How to redirect your website to a new domain/location in .htaccess

How to view all installed PHP modules?

How to Extract Query String (from a URL) Into an Associative Array in PHP

What are the predefined variables in PHP?

How to add a name prefix to all routes in a group?

How to find the intersection (same values) from two arrays?

Force trailing slash (redirect if no trailing slash exists) in .htaccess

What is WordPress default Apache .htaccess rules?