Cdbcriteria Search In Date

I have a date field and it’s working fine. But now I’m making an advanced search so users can easily search by month or year.

In the model I’ve created 2 public attributes.

public $byMonth;

public $byYear;

I’ve added them to ‘safe’, ‘on’=>‘search’.

This is my search form:

<?php echo $form->label($model,'byMonth'); ?>

<?php echo $form->textField($model,'byMonth',array('maxlength'=>4)); ?>

<?php echo $form->label($model,'byYear'); ?>

<?php echo $form->textField($model,'byYear',array('maxlength'=>4)); ?>

In the month field user will input something like "01" if they are searching for january, "02" for february, etc.

In the year field they will just input the year.

I’ve been trying a few ways to do it with CDbCrieria but I can’t make it work.

Any idea how to solve this?


Something like this?

    $criteria->compare('MONTH(your_date_column)', $this->byMonth);

    $criteria->compare('YEAR(your_date_column)', $this->byYear);

That’ll probably only work without leading zeroes on the month, but it should get you moving in the right direction.

Thank you for the help, it’s working like a charm.

I need to improve my SQL skills :)