2

How does one retrieve users from Craft3 DB whose birthday is today?

Birthdate field type is Date/Time.

Have a working PHP/MySQL example, but unsure how to port this to Craft3 syntax.

$sql = 'SELECT 
            id,
            birthDate
         FROM 
            !
         WHERE 
            status = ?';


$users = $db->getAll($sql, array( USERS_TABLE, 
                                  'active', 
                                  date('j'),
                                  date('n')
                                ) 
                    );  

Craft plugin

$users = \craft\elements\User::find() ->group('siteUsers') ->status('active') ->all();

1 Answer 1

2

Was able to get the results I needed by using the following query:

        $users = \craft\elements\User::find()
            ->where("extract(day from content.field_birthdate) = " . date('j'))
            ->andWhere("extract(month from content.field_birthdate) = " . date('n'))
            ->group('siteUsers')
            ->status('active')
            ->all();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.