date and time compare

Hi, how to solve this?

I need to have an options where user can pull data based from the

  1. the last day it was created

  2. 3 days ago

  3. 5 days ago

  4. 1 week ago

  5. fortnight ago

  6. month ago

  7. quarter ago

8 ) 6 months ago

  1. year ago

  2. year+ ago

and it should be available at my advance search function.

here’s what i have at the moment ( I copied the function of the admin controller + view )




//controller

        public function actionAdvancecvsearch()

        {


            $model = new Wsrecruitcvhead('search');

            $model->unsetAttributes();

            if(isset($_GET['Wsrecruitcvhead'])){

                $model->attributes = $_GET['Wsrecruitcvhead'];


            }

                  $this->render('advancecvsearch',array(

                    'model' => $model,

                ));

       }






//model

        const LAST_DAY = 0;

        const LAST_3_DAYS = 1;

        const LAST_5_DAYS = 2;

        const LAST_WEEK = 3;

        const LAST_FORNIGHT = 4;

        const LAST_MONTH = 5;

        const LAST_QUARTER = 6;

        const LAST_6_MONTHS = 7;

        const LAST_YEAR = 8;

        const LAST_YEAR_BEYOND = 9;







/*

* if I have a DateCreated column in my table

* how will I use the  data from the select option to solve it

*

*/

	public function searchcv($returnRaw = false)

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('ResumeTitle',$this->ResumeTitle,true);

		$criteria->compare('ResumeSummaryIntroduction',$this->ResumeSummaryIntroduction,true);

		$criteria->compare('SummaryOfPositionSought',$this->SummaryOfPositionSought,true);

		$criteria->compare('SummaryOfSkills',$this->SummaryOfSkills,true);

                $criteria->compare('DateCreated',<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?,true);

                if($returnRaw === true){

                    return $criteria;

                }





		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));

	}


        /*

         * display select option time intervals

         */

        public function getTimeOptions()

        {

            return array(

                self::LAST_DAY => 'A day ago',

                self::LAST_3_DAYS => '3 days ago',

                self::LAST_WEEK => 'A week ago',

                self::LAST_FORNIGHT => '2 weeks ago',

                self::LAST_MONTH => 'A month ago',

                self::LAST_QUARTER => 'Last quarter',

                self::LAST_6_MONTHS => '6 months ago',

                self::LAST_YEAR => 'Last year',

                self::LAST_YEAR_BEYOND => 'More than a year ago',

            );

        }









//view

<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>

<?php #echo " | " .CHtml::link('Save Search',array('savesearchresult','r'=>'wsrecruitcvhead/savesearchresult')); ?>

<div class="search-form" style="display:none">

<?php

$this->renderPartial('_mysearch',array(

	'model'=>$model,

));

?>

</div>




<?php

$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'wsrecruitcvhead-grid',

	'dataProvider'=>$model->search(),

	#'filter'=>$model,

	'columns'=>array(

		'ResumeTitle',

		'ResumeSummaryIntroduction',

        	'SummaryOfPositionSought',

		'SummaryOfSkills',


		

                array(

                    'class' => 'CButtonColumn',

                    'viewButtonUrl' => 'Yii::app()->createUrl("wsrecruitcvhead/view",array("id"=>$data["ResumeID"]))',

                    'template'=>'{view}',

                ),

	),

));

?>






//_mysearch view file

<div class="wide form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'action'=>Yii::app()->createUrl($this->route),

	'method'=>'get',

)); ?>

        <div class="row">

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

                <?php echo $form->dropDownList($model,'DateCreated', $model->getTimeOptions()); ?>

        </div>


	<div class="row">

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

		<?php echo $form->textField($model,'ResumeTitle',array('size'=>60,'maxlength'=>255)); ?>

	</div>


	<div class="row">

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

		<?php echo $form->textArea($model,'ResumeSummaryIntroduction',array('rows'=>6, 'cols'=>50)); ?>

	</div>

	<div class="row">

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

		<?php echo $form->textArea($model,'SummaryOfPositionSought',array('rows'=>6, 'cols'=>50)); ?>

	</div>


	<div class="row">

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

		<?php echo $form->textArea($model,'SummaryOfSkills',array('rows'=>6, 'cols'=>50)); ?>

	</div>

	<div class="row buttons">

		<?php echo CHtml::submitButton('Search'); ?>

	</div>


<?php $this->endWidget(); ?>




You’ll need an additional property in your model to receive the user input which specifies the search condition about the created date.




//model

    ...

    public $time_cmp_option = self::LAST_DAY;

    ...



And in the search form, we will use ‘time_cmp_option’ property instead of ‘DateCreated’.




// search form

<div class="row">

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

    <?php echo $form->dropDownList($model,'time_cmp_option', $model->getTimeOptions()); ?>

</div>



This property has no entity in the db table. But we must include it in the rules for search.




// model

public function rules()

{

    return array(

        ...

        array('id, date, type_id, ..., time_cmp_option', 'safe', 'on'=>'search'),

    );

}



And then we can construct the search criteria for ‘DateCreated’ according to the user input like this, assuming ‘DateCreated’ is stored as an unix time.




public function searchcv($returnRaw = false)

{

    $criteria=new CDbCriteria;


    $criteria->compare('ResumeTitle',$this->ResumeTitle,true);

    ...

    if ( $this->time_cmp_option == self::LAST_3_DAYS )

    {

        $criteria->compare('DateCreated', '<' . (time()-60*60*24*3), true);

    } 

    else if ( $this->time_cmp_option == self::LAST_5_DAYS )

    {

        $criteria->compare('DateCreated', '<' . (time()-60*60*24*5), true);

    }

    ...

}



You may want to change the comparing statements.

there’s a problem,

  1. my DateCreated data was saved as DATETIME e.g 2011-01-01 00:00:00

  2. when i select a different option from the drop down and viewed the selected value on firebug,

the selected value doesn’t change. it’s stuck with the “0” value , maybe because of the assigning of

time_cmp_option = self::LAST_DAY ?

I tried this on the model but it doesn’t pull out the correct criteria by just selecting A DAY AGO or 3 DAYS AGO




//model

		$criteria=new CDbCriteria;

		$criteria->compare('Res`umeTitle',$this->ResumeTitle,true);

		$criteria->compare('ResumeSummaryIntroduction',$this->ResumeSummaryIntroduction,true);

		$criteria->compare('SummaryOfPositionSought',$this->SummaryOfPositionSought,true);

		$criteria->compare('SummaryOfSkills',$this->SummaryOfSkills,true);

                if($this->time_cmp_option == self::LAST_DAY)

                {

                    $criteria->compare('DateCreated','='. strtotime('now -1 day'), true);

                }

                elseif($this->time_cmp_option == self::LAST_3_DAYS)

                {

                    $criteria->compare('DateCreated','<'. 60*60*24*3,true);

                }

                

                        if($returnRaw === true){

                        return $criteria;

                    }

		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));



Comparing DATETIME is sometimes very cumbersome … so I assumed unix time ;)

If your DateCreated always has zeroed hours, minutes and seconds, then comparing statements might be something like this …




if($this->time_cmp_option == self::LAST_DAY)

{

    $criteria->compare('DateCreated','='. date('Y-m-d 00:00:00', strtotime('now -1 day')), true);

}

elseif($this->time_cmp_option == self::LAST_3_DAYS)

{

    $criteria->compare('DateCreated','<'. date('Y-m-d 00:00:00', now() - 60*60*24*3), true);

}



I hope someone will show us a better way.

On the other problem with the dropDownList, I’m sorry I have no idea.

I want to use the between to filter by date? Please help, thanks a lot.