0

Just wanted to find out what is the best way to calculate age from DOB on the fly using the Yii framework.

I have a model with the following fields.

        'id' => 'ID',
        'firstname' => 'Firstname',
        'surname' => 'Surname',
        'dob' => 'Dob',
        'age' => 'Age',

Then in my _form.php file I am trying to edit it so that the age is calculated on the fly

<?php $form = ActiveForm::begin(); ?>

<?php
    $currentDate=date('Y-m-d');
    $dob=$model->dob; //Careful with initialization???

    $model->age=$currentDate-$dob; 
?>

<?= $form->field($model, 'firstname')->textInput(['maxlength' => 50]) ?>

<?= $form->field($model, 'surname')->textInput(['maxlength' => 50]) ?>

<?= $form->field($model, 'dob')->widget(
    DatePicker::className(), [
    // inline too, not bad
    'inline' => false, 
    // modify template for custom rendering
    'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
    'clientOptions' => [
        'autoclose' => true,
        'format' => 'yyyy-mm-dd'
    ]
]);?>

<?php echo Html::activeHiddenInput($model, 'age') ; 

<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

Of course there is a problem here as to get the $model->dob, the form first has to be submitted, so when the calculation of age is done dob is uninitiated.

What I want to know is there someway I can read from the dob field first so that the age can be computed correctly?

Or is this best done by something like a trigger in the database?

Looking forward to hearing responses.

Cheers.

Winkmei5ter
  • 318
  • 5
  • 18
  • Why you are including hidden input with age in your form? And such calculations should be done in model instead of view. – arogachev Mar 17 '15 at 10:09
  • You can do this calculation in beforeSave method. It gets called before the insert event. For detail here is the link: http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#beforeSave%28%29-detail – Chinmay Waghmare Mar 17 '15 at 10:16
  • Like @arogachev said, you should calculate the age in a function like "getAge()". It's not pertinent to store a calculable. – Timothée Planchais Mar 17 '15 at 10:42
  • This is not yii specific and can be handled in your controller or model (depending on your choice) using pure php, look at the answer [here](http://stackoverflow.com/a/3923228/724913) for the code – arkoak Mar 17 '15 at 11:19

1 Answers1

1

In your user model, add:

public $age;

public function getAge()
{
    // age in format yyyy-mm-dd
    if(isset($this->age)) return $this->age;
    else {
        $dob = explode("-", $this->dob);
        $age = (date("md", date("U", mktime(0, 0, 0, $dob[1], $dob[2], $dob[0]))) > date("md")
            ? ((date("Y") - $dob[2]) - 1)
            : (date("Y") - $dob[2]));
        $this->age = $age;
        return $age;
    }
}

Then you can just call $model->getAge() where you need it. (If the $model is your user model ofcourse.)

Jørgen
  • 3,467
  • 6
  • 33
  • 49