XF 2.1 xF Finder: select Users with age between x and y years

Scandal

Well-known member
Hello all!
I want to use xF2 Finder to detect accounts with member age (in years), between X and Y.

Let's say that we want to detect the accounts that have age between 18 and 21 years old.
How exact do I have to edit the finder?
PHP:
$finder = \XF::finder('XF:UserProfile')->with('User');
I used XF:UserProfile cause its table has the following columns:
  • dob_day
  • dob_month
  • dob_year
Important notice: I know that there are the following finder/ entity methods:
->getAge()
Currently I'm doing this:
PHP:
        foreach ($users AS $u)
        {
            if ($u->getAge(true) >= 18 && $u->getAge(true) <= 21)
            {
                $output[] = $u->User;
            }
        }

... but I want deprecate that code and starting initially to select the members between 18 and 21 years (= where 18 and 21, I will be able to put any value).
So I don't want to fetch more users and checking via php to limit the results like the above code.
Any way to select them directly via the $finder?
 
Fully untested but smth. along those lines could work

PHP:
$finder
    ->where('dob_month', '>', 0)
    ->where('dob_day', '>', 0)
    ->where('dob_year', '>', 0)
    ->whereSql(sprintf(
        "TIMESTAMPDIFF(YEAR, CONCAT(%s, '-', %s, '-', %s), NOW()) BEWTWEEN %d AND %d",
        $finder->columnSqlName('dob_year'),
        $finder->columnSqlName('dob_month')
        $finder->columnSqlName('dob_day'),
        $minAge,
        $maxAge
    ));
 
Back
Top Bottom