Today we will see examples of using whereDate and whereDay query. We are going to learn about eloquent query whereDate and whereDay in Laravel with examples below. Look at the examples to see how different using whereDate() and whereDay().
We are going to use below table as sample records to manipulate testing and see how it works. So first please see below table with some record
whereDate() - is used to make condition and compare column's value to datatype date or timestamps.
Example #1
// Return records that have created_at column's value equal to 2022-07-15
$users = User::whereDate('created_at', '=', '2022-07-15')
->get();
foreach($users as $user)
{
echo $user->name." | ".$user->created_at."<br>";
}
Output #1
User | 2022-07-15 14:26:26
Editor | 2022-07-15 14:26:26
Admin | 2022-07-15 14:26:26
Exmple #2
// Return records that have created_at column's value equal or less than 2018-01-01
$users = DB::table('users')
->whereDate('created_at','<=','2018-01-01')
->get();
foreach($users as $user)
{
echo $user->name." | ".$user->created_at."<br>";
}
Output #2
Prof. Tomas Glover II | 2017-11-20 12:47:53
Ressie Feest | 2014-05-20 12:47:53
whereDay() - is used to make condition and compare a specific day of the month.
Example #1
/* Return all record which are created_at column
have day of month equal or greater than 29 */
$users = User::whereDay('created_at', '>=', '29')
->get();
foreach($users as $user)
{
echo $user->name." | ".$user->created_at."<br>";
}
Output #1
Dr. Carmine McCullough | 2022-07-30 12:47:53
Example #2
/* Return all record which are created_at column
have day of month = 01 */
$users = DB::table('users')
->whereDay('created_at', '01')
->get();
foreach($users as $user)
{
echo $user->name." | ".$user->created_at."<br>";
}
Output #2
Helen Pollich | 2022-07-01 12:47:53
Hope these Laravel whereDate and whereDay query builder examples help you to complete your project. Have a nice day!
You might Also Like:
As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him