Saurabh Saurabh - 7 months ago 145
PHP Question

Yii2: How to compare two dates?

In my form

<?= $form->field($model, 'check_in')->input('date', ['required' => false])->label(false); ?>
<?= $form->field($model, 'check_out')->input('date', ['required' => false])->label(false); ?>


there are two fields
check_in
and
check_out
i need to validate this two fields,
check_out
should not be smaller than
check_in
date how to do this?

Answer

You can use the compare validator in your model's rules function:

['check_in', 'compare', 'compareAttribute' => 'check_out', 'operator' => '<='],

This will resolve to $model->check_in <= $model->check_out