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 get table column names from a database table in Eloquent?

When is __destruct called in PHP?

What is the difference between ( for ... in ) and ( for ... of ) in javascript?

How to replace whitespace with a single space

How to block an IP address in .htaccess

How to select what columns to return when calling ::all()

How do you run a PHP script from the command line?

How to replace values in a string, in JS

How to Enable Directory Listings in Apache with .htaccess

How to sort an array in javascript?