how to get retirement of this month by date of birth

Viewed 126

I want to show retirement of current month by date of birth. I have a column dob (format is 1960-12-21). Now I have to calculate 60 years from dob by current month. If current month is October 2020 and date of birth is 1960-09-21 then records should be fetch. Today's date doesn't matter. But if month is September 2020 OR November 2020 then data should not show.

Records should be fetch by current month, if current month completes 60 years.

Means results should be:

Retirement of this month is: abc,xyz etc.

I tried, but I am not getting my results:

Controller is:

 public function index()
    {
        $retirement = User::select('name','dob')->get();
        foreach($retirement as $retirement)
        {
            echo Carbon::parse($retirement->dob)->age;
          
        }
    }

It shows years only.

1 Answers

Use whereMonth() and whereYear() to find the current month and where the year is 60 years ago. You can achieve this by getting now() and then accessing the month and year properties respectively.

$retirement = User::whereMonth('dob', now()->month)
                  ->whereYear('dob', now()->subYears(60)->year)
                  ->get();
Related