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

What are route patterns in Laravel?

How to find the average value of items in a Laravel Collection?

What is the null coalescing operator in PHP7?

How to get the first row that matches some where queries, or create it if it doesn't exist (in Laravel's Eloquent)?

What does PEAR stand for?

What is Bubble sort, and how to write an implementation of it in PHP

How to enable the rewrite engine in .htaccess?

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

How to block IPv6 IP addresses in .htaccess

How to make Apache force download of files (in .htaccess)