Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tomasosho's avatar

how to get date in year and month format e.g 2017-04

How can i get my date in year and month format?

$gg = Attendance::find($request->input('user_id'))->get('date', '=', format('Y-m'));
0 likes
21 replies
tykus's avatar

If you have date already as a Carbon instance (e.g. it is cast to a datetime in the Attendance model)

$gg = Attendance::find($request->input('user_id'))->date->format('Y-m');

Otherwise, make it a Carbon instance:

$gg = \Carbon\Carbon::parse(Attendance::find($request->input('user_id'))->date)->format('Y-m');
1 like
tomasosho's avatar

How do i apply it here?

$attendance = \Carbon\Carbon::parse(Attendance::find($request->input('user_id')))->whereDate('date', format('Y-m'), '=', $request->input('month_year'))->count('present');
tykus's avatar

Ok, that is a different problem, which you could solve like this:

[$year, $month] = explode('-', $request->input('month_year')); // e.g. [2021, 7]
$attendance = Attendance::selectRaw('count(present) as present_count')
	->whereMonth('date', $month)
	->whereYear('date', $year)
	->where('user_id', $request->input('user_id'))
	->first();
tomasosho's avatar

i am getting

ErrorException
Undefined offset: 1

for

[$year, $month] = explode('-', $request->input('month_year'));
tykus's avatar

What is the format of $request->input('month_year’)?

tykus's avatar

The explode expression should make two variables in that case. Maybe wrap in try/catch an bail out if the year and month are not set

tomasosho's avatar

It's returning my present count() as 0

$date = MonthYear::find($request->input('month_year'));
        // [$year, $month] = explode('-', $request->input('month_year')); // e.g. [2021, 7]
        $attendance = Attendance::selectRaw('count(present) as present_count')
            ->whereDate('date', $date)
            ->where('staff_id', $request->input('user_id'))
            ->get();
tykus's avatar

Why not whereMonth and whereYear???

tomasosho's avatar

I was getting same results, It’s not counting not null values under Present

tykus's avatar

Can you dump the query that is actually being executed; whenever you use whereMonth and whereYear; use the following immediately before the query above:

DB::listen(fn ($query) => dump([$query->sql, $query->bindings]));
tomasosho's avatar

I don't understand how to apply it. can you expatiate a bit?

tomasosho's avatar

It works this way, but upon adding the whereMonth and whereYear, the counting returns to 0

$attendance = Attendance::selectRaw('count(present) as present_count')
            // ->whereMonth('date', $month)
            // ->whereYear('date', $year)
            ->where('staff_id', $request->input('user_id'))
            ->get();
tykus's avatar

This is how you would implement the Query listener (temporarily) - it should dump the query SQL and bindings. It will hopefully provide some clue why the query is returning 0 when the Month and Year constraints are applied:

DB::listen(fn ($query) => dump([$query->sql, $query->bindings]));

$attendance = Attendance::selectRaw('count(present) as present_count')
            ->whereMonth('date', $month)
            ->whereYear('date', $year)
            ->where('staff_id', $request->input('user_id'))
            ->first();
tomasosho's avatar

i get this before the main query

array:2 [▼
  0 => "select count(present) as present_count from `attendance` where month(`date`) = ? and `staff_id` = ?"
  1 => array:2 [▼
    0 => "{"id":1,"working_days":"26","month_year":"2021-06","created_at":"2021-07-08T02:01:42.000000Z","updated_at":"2021-07-08T02:14:51.000000Z"}"
    1 => "1"
  ]
]
tykus's avatar

Ok, clearly this "{"id":1,"working_days":"26","month_year":"2021-06","created_at":"2021-07-08T02:01:42.000000Z","updated_at":"2021-07-08T02:14:51.000000Z"}" is not a month number! How are you getting the month number???

tomasosho's avatar
$date = MonthYear::find($request->input('month_year'));

DB
"id" => 1
    "working_days" => "26"
    "month_year" => "2021-06"
    "created_at" => "2021-07-08 02:01:42"
    "updated_at" => "2021-07-08 02:14:51"
tomasosho's avatar

Fixed it!

$date = MonthYear::find($request->input('month_year'));
        [$year, $month]= explode('-', $date->month_year);// e.g. [2021, 7]

        $attendance = Attendance::selectRaw('count(present) as present_count')
            ->whereMonth('date', $month)
            ->whereYear('date', $year)
            ->where('staff_id', $request->input('user_id'))
            ->get();

Thank you!

tykus's avatar
tykus
Best Answer
Level 104

What is this MonthYear class; and why is it necessary in this context? You mentioned earlier that the $request->input('month_year') was in the form 2021-06 already, right? Was it an issue that the month number was 06 rather than 6; did it simply require casting to int:

[$year, $month]= explode('-', $request->input('month_year'));// e.g. [2021, 7]

$attendance = Attendance::selectRaw('count(present) as present_count')
    ->whereMonth('date', (int) $month)
    ->whereYear('date', (int) $year)
    ->where('staff_id', $request->input('user_id'))
    ->get();
tomasosho's avatar

Yes it was an issue with month number 06. how do i get my $attendance value as (int) present_count to be used outside the query. E.g $attendance->(int) present_count to give me 2 just the int.

tykus's avatar

Try to use count Builder method again:

$attendance = Attendance::whereMonth('date', (int) $month)
    ->whereYear('date', (int) $year)
    ->where('staff_id', $request->input('user_id'))
    ->count('present');
1 like

Please or to participate in this conversation.