How to Get current date, week, month, year wise data in Laravel
Laravel

How to Get current date, week, month, year wise data in Laravel

Hi,

In this tutorial – I am sharing with you how to get current date, current week, current month, and current year-wise data in laravel. Let's see,

Current Date Record

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

Output:

Array
(
    [0] => Array
        (
            [name] => maksud
            [created_at] => 2022-04-08 17:58:41
        )
 
)

 

Get Current Week Data

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

 

Output: 

 

Array
(
    [0] => Array
        (
            [name] => Shovon
            [created_at] => 2022-04-08 00:38:03
        )
 
    [1] => Array
        (
            [name] => Asif
            [created_at] => 2022-04-08 05:13:17
        )
 
    [2] => Array
        (
            [name] => Shajjad
            [created_at] => 2022-04-08 05:21:20
        )
 
    [3] => Array
        (
            [name] => Maksud
            [created_at] => 2022-04-08 05:34:00
        )
 
    [4] => Array
        (
            [name] => Noman
            [created_at] => 2022-04-08 06:00:04
        )
  [5] => Array
        (
            [name] => Akash
            [created_at] => 2022-04-08 06:00:24
        )
 )

 

To Get Current Month Data

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

 

Output: 

 

Array
(
    [0] => Array
        (
            [name] => Maksud
            [created_at] => 2022-04-08 17:58:41
        )
 
)

 

Get Month Wise Current Year Data

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

 

Output:

Array
(
    [0] => Array
        (
            [count] => 19
            [monthname] => January
        )
 
    [1] => Array
        (
            [count] => 22
            [monthname] => Feburary
        )
[2] => Array
        (
            [count] => 70
            [monthname] => March
        )
 
)

 

Get Day Wise Current Week Data

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

 

Output:

Array
(
    [0] => Array
        (
            [count] => 45
            [dayname] => Monday
        )
 
    [1] => Array
        (
            [count] => 41
            [dayname] => Tuesday
        )
 
    [2] => Array
        (
            [count] => 12
            [dayname] => Wednesday
        )
 
)

 

Get Data Year Wise

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

 

Output:

Array
(
    [0] => Array
        (
            [count] => 120
            [year] => 2020
        )
 
    [1] => Array
        (
            [count] => 618
            [year] => 2021
        )
 
)

 

I hope, it will you to generate multiple reports according to your data.

Get The latest Coding solutions.

Subscribe to the Email Newsletter