Yii2 how to insert the parameters into the function?

Still yesl how to filter. Here is the code
$query=Direction::find()->joinWith(['pacients' => function ($q) {
$q->where('pacients.dr="1990-02-25"');
 }]);

I need to filter out all who are 18 years of age.
$yearOfBirth = 18;
 $date1 = date((date('Y') - $yearOfBirth - 1) . '-m-d');
 $date2 = date((date('Y') - $yearOfBirth) . '-m-d');
 $query=Direction::find()->joinWith(['pacients' => function ($q, $date1, $date2) {
 $q->where("pacients.dr BETWEEN $date1 AND $date2");
 }]);

Function starts to swear on the parameters $date1, $date2. Tell me how to do it?
June 8th 19 at 17:30
1 answer
June 8th 19 at 17:32
Solution
$yearOfBirth = 18;
$date1 = date((date('Y') - $yearOfBirth - 1) . '-m-d');
$date2 = date((date('Y') - $yearOfBirth) . '-m-d');
$query=Direction::find()->joinWith(['pacients' => function ($q) use ( $date1, $date2) {
 $q->where("pacients.dr BETWEEN :d1 AND :d2",["d1"=>$date1,"d2"=>$date2]);
}]);
Thank you!!!:) - Kade70 commented on June 8th 19 at 17:35

Find more questions by tags Yii