title: Yii date, hour, minute -> datetime string -> MySQL datetime
body: <p>I'm using Yii to construct a web application.  One of my input forms has a CJuiDatePicker for the date.  I have two drop down lists, one for the hours, and one for the minutes.</p>
  
  <p>My problem is in the data model, where I'm trying to convert the date, hour, and minute from the form into a MySQL datetime string.  I have to produce a datetime string that looks like this - <code>2011-02-27 20:11:56</code>, so Yii can convert the string into a MySQL datetime and insert the value into the row.</p>
  
  <p>In the model, I have a rule that looks like this:</p>
  
  <pre><code>array('event_datetime_from', 'createDatetime', 
      'date'=&gt;'event_date_from', 'hour'=&gt;'event_hour_from',
      'minute'=&gt;'event_minute_from'),
  </code></pre>
  
  <p>The <code>createDateTime</code> validator function looks like this:</p>
  
  <pre><code>public function createDatetime($attribute, $params) {
      if (!$this-&gt;hasErrors()) {      
          $date = $this-&gt;$params['date'];
          $hour = $this-&gt;$params['hour'];
          $minute = $this-&gt;$params['minute'];
  
          if (trim($date) === '') {
              $this-&gt;$attribute = null;
          } else {
              $parse = CDateTimeParser::parse(
                  $date.' '.$hour.':'.$minute,
                  'MM/dd/yyyy hh:mm');
              $this-&gt;$attribute = date('Y-m-d H:i:s', $parse);
          }
      }
  }
  </code></pre>
  
  <p>Now, I'm not a PHP developer.  However, it appears to me that <code>$params['date']</code> is returning the string value <code>event_date_from</code>, rather than the value of <code>event_date_from</code>.</p>
  
  <p>My PHP question is, how do I get the value of <code>event_date_from</code> inside the <code>createDateTime</code> validator function?</p>
  
  <p>My apologies if I overlooked the answer somewhere in the Yii documentation.  I couldn't find many examples of validator functions.  The Yii validator classes have a different parameter signature than validator functions.</p>
  
  <p>Edited based on thaddeusmt's answer:</p>
  
  <p>I tried extending CActiveRecord and coded an afterValidate method, but I couldn't find a place to define my working date, hour, and minute variables.  I defined them in the extended method, and the afterValidate method couldn't see them.  I got a PHP undefined variable error in the afterValidate method.</p>
  
  <p>In the controller, I coded the following function:</p>
  
  <pre><code>protected function createDateTime($dateString, $hour, $minute) {
      if (trim($dateString) == '') {
          return null;
      } else {
          $timeString = $dateString.' '.$hour.':'.$minute;
          return date('Y-m-d H:i:s', strtotime($timeString));
      }
  }
  </code></pre>
  
  <p>It should be a cinch to call a function in PHP, right?</p>
  
  <p>I tried both of these calls in the actionCreate() function:</p>
  
  <pre><code>$model-&gt;event_datetime_from = 
              createDateTime($_POST['event_date_from'],
              $_POST['event_hour_from'],
              $_POST['event_minute_from']
              );
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>$model-&gt;event_datetime_from = 
              createDateTime($model-&gt;event_date_from,
              $model-&gt;event_hour_from,
              $model-&gt;event_minute_from
              );
  </code></pre>
  
  <p>My controller code dies with either of these calls, and I get a blank (no HTML) response page.</p>
  
  <p>I thought what I wanted to do was pretty simple.  I want to take a date, hour, and minute string, and convert the concatenation to a datetime string.  What am I missing?</p>