Laravel Get Current Date, Week, Month, YEAR Wise Data

Laravel get current date, week, month, year wise data; In this tutorial, i am going to show you how to get current date, current week, current, month, current year data in laravel apps.

Laravel Get Current Date, Week, Month, YEAR Wise Data

Use the below methods to get get current date, month, week, and year data in laravel 9 apps:

  • 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

To fetch the current date records from database tables in laravel; as follows:

User::whereDate('created_at', Carbon::today())->get(['name','created_at']);

Get Current Week Data in Laravel

To get the current week data from the MySQL database table; as follows:

$current_week = User::whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])->get();

To Get Current Month Data in Laravel

To get current month records from date; as follows:

User::whereMonth('created_at', date('m'))
->whereYear('created_at', date('Y'))
->get(['name','created_at']);

Laravel Get Month Wise Current Year Data

To get current year data month wise from the database table; as follows:

User::select(DB::raw("(COUNT(*)) as count"),DB::raw("MONTHNAME(created_at) as monthname"))
->whereYear('created_at', date('Y'))
->groupBy('monthname')
->get();

Get Day Wise Current Week Data Laravel

To get day-wise current week data from the database table; as follows:

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();
;

Laravel Get Data Year Wise

To get or fetch year wise data from database; as follows:

User::select(DB::raw("(COUNT(*)) as count"),DB::raw("YEAR(created_at) as year"))
->groupBy('year')
->get();

Recommended Laravel Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.