title: Searching users by age range based on a birthdate in a database table
body: <p>Now, I have a database table which stores information about users along with their birhtdate in the "datetime" format. This database table correlates with a respective ActiveRecord model. </p>
  
  <p>In the user search form I want to implement search by age range (with two form dropdown fields, "from" and "to" age respectively).</p>
  
  <p>I take it I have to convert age submitted via form into the actual "datetime" date before executing search against the database table.</p>
  
  <p>I still don't have enough understanding of CDb Criteria and Yii in general. It seems like a common thing in social networks, but I haven't been able to find a sufficient answer to my question.  </p>
  
  <p>I kinda found a solution while I was waiting for some reply. But I figure I might not have been thinking straight so I probably came up with something not quite reasonable. But it works.</p>
  
  <p>I created two variables in my User model:</p>
  
  <pre><code>        public $fromBirthdate;
          public $toBirthdate;
  </code></pre>
  
  <p>I changed the search fields in the search form from a single birthdate field to the fields "to" and "from", which hold integers signifying minimum and maximum age respectively. </p>
  
  <p>I tied those fields to the newly created variables in the model.  </p>
  
  <pre><code>&lt;div class="row"&gt;
          &lt;?php echo $form-&gt;label($model,'fromBirthdate'); ?&gt;
          &lt;?php echo $form-&gt;textField($model,'fromBirthdate'); ?&gt;
      &lt;/div&gt;
  
      &lt;div class="row"&gt;
          &lt;?php echo $form-&gt;label($model,'toBirthdate'); ?&gt;
          &lt;?php echo $form-&gt;textField($model,'toBirthdate'); ?&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>></p>
  
  <p>Then I wrote a function which transforms age into birthdate based on the current time:</p>
  
  <pre><code>//transforms age into birthdate based on current date
  //@param integer age
  //@param integer default age (what age should be applied in case the age parameter is null)
  //$returns string date formatted MySQL timestamp
  
  public function getBirthdate($age, $defaultAge)
  {
      if($age == null)
      $age = $defaultAge;
  
      $birthDate = date('Y-m-d H:i:s',strtotime("-" . $age . "years", time()));
      return $birthDate;
  }
  </code></pre>
  
  <ul>
  <li>I fiddled a little with the instance of CDbCriteria in the search() function of the model by adding the following line:</li>
  </ul>
  
  <p><pre><code> 
  //specifies range of age the users should be searched within
      //if either of the age fields ("to" or "from" age") was not filled, default age is applied
      //default age range is from 10 to 110 years old. 
      $criteria->addBetweenCondition('birthdate', $this->getBirthdate($this->toBirthdate, 110), $this->getBirthdate($this->fromBirthdate, 10));
  </pre></code></p>
  
  <p>All in all, when a user submits age range via the search form, it gets stored in those two variables as integers. Then they get overwritten with the strings containing datetime stamps which are transformed from the age submitted. </p>
  
  <p>I had to introduce so called "default min and max age values into the play" in case a user didn't specify min and max ages on search. I am not sure this one was the wisest idea ever, but it works fine with me. </p>