In this tutorial you will learn about the Laravel Get Current Date, Week, Month Wise, YEAR Data and its application with practical example.
In this Laravel Get Current Date, Week, Month Wise, YEAR Data tutorial I will show you how to get current date, current week, current, month, current year data in laravel. In this tutorial you will learn to get current date, current week, current, month, current year data in laravel.
Laravel Get Current Date, Week, Month Wise, YEAR Data
Table Of Content
- Current Date Record Laravel
- Get Current Week Data in Laravel
- To Get Current Month Data in Laravel
- Laravel Get Month Wise Current Year Data
- Get Day Wise Current Week Data Laravel
- Laravel Get Data Year Wise
Current Date Record Laravel
If you want to fetch records for current date from database tables. Provided eloquent Query can be used to fetch records for current date.
1 |
User::whereDate('created_at', Carbon::today())->get(['name','created_at']); |
The output of the above laravel eloquent query looks like:
1 2 3 4 5 6 7 8 9 |
Array ( [0] => Array ( [name] => dsfds [created_at] => 2019-12-08 17:58:41 ) ) |
Get Current Week Data in Laravel
If you want to fetch records for current week from database tables. Provided eloquent Query can be used to fetch records for current week.
1 |
$current_week = User::whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])->get(); |
This query uses laravel eloquent method whereBetween().
The output of the above laravel eloquent query looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
Array ( [0] => Array ( [name] => Gowsegan [created_at] => 2019-12-09 00:38:03 ) [1] => Array ( [name] => Brookside Dairy Best [created_at] => 2019-12-09 05:13:17 ) [2] => Array ( [name] => abc [created_at] => 2019-12-09 05:21:20 ) [3] => Array ( [name] => test [created_at] => 2019-12-09 05:34:00 ) [4] => Array ( [name] => StartBit Solutions [created_at] => 2019-12-09 06:00:04 ) ) |
To Get Current Month Data in Laravel
If you want to fetch records for current month from database tables. Provided eloquent Query can be used to fetch records for current month.
1 2 3 |
User::whereMonth('created_at', date('m')) ->whereYear('created_at', date('Y')) ->get(['name','created_at']); |
This query users laravel method whereMonth, whereYear and get().
The output of the above laravel eloquent query looks like:
1 2 3 4 5 6 7 8 9 |
Array ( [0] => Array ( [name] => dsfds [created_at] => 2019-12-08 17:58:41 ) ) |
Laravel Get Month Wise Current Year Data
If you want to fetch month wise records from database tables. Provided eloquent Query can be used to fetch month wise records.
1 2 3 4 |
User::select(DB::raw("(COUNT(*)) as count"),DB::raw("MONTHNAME(created_at) as monthname")) ->whereYear('created_at', date('Y')) ->groupBy('monthname') ->get(); |
This query uses db::raw(), whereYear(), and groupBy() methods.
Output of the above laravel eloquent query is look like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Array ( [0] => Array ( [count] => 9 [monthname] => February ) [1] => Array ( [count] => 2 [monthname] => September ) ) |
Get Day Wise Current Week Data Laravel
If you want to fetch day wise records from database tables. Provided eloquent Query can be used to fetch day wise records.
1 2 3 4 5 6 |
User::select(DB::raw("(COUNT(*)) as count"),DB::raw("DAYNAME(created_at) as dayname")) ->whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()]) ->whereYear('created_at', date('Y')) ->groupBy('dayname') ->get(); ; |
The output of the above laravel eloquent query looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Array ( [0] => Array ( [count] => 45 [dayname] => Monday ) [1] => Array ( [count] => 41 [dayname] => Tuesday ) [2] => Array ( [count] => 12 [dayname] => Wednesday ) ) |
Laravel Get Data Year Wise
If you want to fetch year wise records from database tables. Provided eloquent Query can be used to fetch year wise records.
1 2 3 |
User::select(DB::raw("(COUNT(*)) as count"),DB::raw("YEAR(created_at) as year")) ->groupBy('year') ->get(); |
The output of the above laravel eloquent query looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Array ( [0] => Array ( [count] => 6 [year] => 2018 ) [1] => Array ( [count] => 9618 [year] => 2019 ) ) |