Friends, the where condition is very important for any SQL query because only on the basis of the where condition we can get this specific data, whether it is on the basis of a user, on the basis of a specific date, on the basis of a specific keyboard or on the basis of any other thing, the where condition is very important.
In today’s tutorial we will learn how to use where condition with date and time:
$users = DB::table('users') ->whereDate('created_at', '2016-12-31') ->get();
$users = DB::table('users') ->whereMonth('created_at', '12') ->get(); $users = DB::table('users') ->whereDay('created_at', '31') ->get();
$users = DB::table('users') ->whereYear('created_at', '2016') ->get();
$users = DB::table('users')
->whereTime('created_at', '=', '11:20:45')
->get();
Get data between two dates
$startDate = Carbon::createFromFormat('Y-m-d', '2022-06-01');
$endDate = Carbon::createFromFormat('Y-m-d', '2022-06-30');
$users = User::whereDate('start_at', '>=', $startDate)
->whereDate('end_at', '<=', $endDate)
->get();