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

JackD's avatar

match age range to birthdate in database for search filter

im creating search filter problem is that the age range slider gets example (18,20) but what i have in my database table of user is birthdate column example (1998-05-11)

that's why im getting error because 18,20 age range can't be matched with 1998-05-11 birthdate format

i already add this in my controller

` ->where('birthday', 'BETWEEN', Input::get('age-range'))

note: i already explode and implode the age range to get a format like (18 AND 27)

0 likes
10 replies
JarekTkaczyk's avatar
Level 53

This includes people who are already 18 and not yet 20

// explode the range and set as follows
$minAge = 18;
$maxAge = 20;

// prepare dates for comparison
$minDate = Carbon::today()->subYears($max); // make sure to use Carbon\Carbon in the class
$maxDate = Carbon::today()->subYears($min)->endOfDay();

// then add to the query
whereBetween('birthday', [$minDate, $maxDate])

If you want inclusive range, then adjust tha $minDate as below - then you will get people who are 18 through 20+, but not yet 21:

$minDate = Carbon::today()->subYears($max + 1);
7 likes
JackD's avatar

@JarekTkaczyk thanks! it works, but i have a little problem, the user has 1970-01-01 and i set the range to 18,45 the user must be in the list but he's not there, i have to adjust the age range to 18-46 and the user with 1970-01-01 showed up. is there any fix for that little problem?

JarekTkaczyk's avatar

@ShutteR It doesn't help much. None of suggested functions is applicable here and there are a few drawbacks:

User::whereRaw( 'timestampdiff(year, birthday, curdate()) between ? and ?', [$minAge, $maxAge] );
  1. it requires raw statements
  2. it is MySQL specific only
  3. it doesn't seem to be easier to read at all
JarekTkaczyk's avatar

@dinis But it's out of range - the guy is 45 yrs and 2 months old - how would you like to include him in the result? Do you want to take into consideration only YEAR part?

@dinis Check the edited answer above - I don't think it's right to make the range inclusive, but I suppose this is what you want.

JackD's avatar

@JarekTkaczyk yup you're right i don't want to make it range inclusive. but the user is already in range if the user's birthdate is 1970-01-01 the user just turned 45 this year january 1, that's why the user must be in the list within the range 18-45.

the problem if i add $max+1 that will be year basis even the birthdate month and date is not yet reached by the server date

JarekTkaczyk's avatar

@dinis Well, it's not logical, 1970-01-01 is not in the range. 1970-02-22 would be first day in range.

Describe exactly the requested behaviour with a few examples. For instance, what about 1969-12-31 ? Or would you like to check only year not the fulll date?

JackD's avatar

@JarekTkaczyk i have two more question, how can i except from search result list the user who did the search filter? example i logged in and then i did search filter, my name is included in the list how to not show my name in the list?

i forgot to include paginating the search results

bobbybouwmann's avatar

You can do something like

// id is here the user id of course.
->where('id', '!=, Auth::user()->id)
1 like

Please or to participate in this conversation.