0

I have the field date_start field which is a date datatype. I want to get the month and year of the field date_start and check the month is in given date month '2021-02-01'.

$meeting_ids=DB::table('meeting_dump')
    ->Where(DB::raw(date('m-y',strtotime('meeting_dump.date_start'))),'=',date("m-y", strtotime($dates_between[0])))
    ->pluck('meeting_dump.parent_id')
    ->toArray();

given date is 2021-02-01 and meeting start date is 2021-02-01, 2021-01-05 need to pick the parent_id for 2021-02-01

SEYED BABAK ASHRAFI
  • 4,093
  • 4
  • 22
  • 32
user3386779
  • 6,883
  • 20
  • 66
  • 134
  • use `->whereMonth('date_start', '=', '02')->whereYear('start_date', '=', '2021')` for february 2021 – STA Feb 07 '21 at 09:15

2 Answers2

1

You can do it with Carbon:

(new Carbon('2021-02-01'))->format("m-y") //this will yield 02-21

You can do the same for your date field.

Silidrone
  • 1,471
  • 4
  • 20
  • 35
0

For your problem I would suggest to create date range of month start and month end from your input date instead of formatting the dates on fly

$date = \Carbon\Carbon::parse("2021-02-01");
$start = $date->startOfMonth()->format('Y-m-d H:i:s');
$end = $date->endOfMonth()->format('Y-m-d H:i:s');

And in query builder use between filter the benefit of this approach is your query remains sargable means if there is an index added on this column it will use that index but if you perform formatting on your date column then index will get ignored

$meeting_ids= DB::table('meeting_dump')
                ->whereBetween('date_start', [$start, $end])
                ->pluck('parent_id')
                ->toArray();
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118