How to use the CJUIDatePicker for DOB and how to store datetime into tables?

hi i m having problem in using the juidatepicker the date of birth in the user table and storing the date time in table

here is the view.


	<div class="row">

		<?php echo $form->labelEx($model,'dob'); ?>

        <?php   $this->widget('zii.widgets.jui.CJuiDatePicker', array(

    'name'=>'dob',

    'model'=>$model,

        

    // additional javascript options for the date picker plugin

    'options'=>array(

        'showAnim'=>'fold',

        'dateFormat'=>'dd/mm/yy',

    ),

    'htmlOptions'=>array(

        'style'=>'height:20px;'

    ),

));

?>

		<?php echo $form->textField($model,'dob'); ?>

		<?php echo $form->error($model,'dob'); ?>



i have two fields in user table which are storing the last active time of user and the user creation time. i used this to store them is this the proper way?.


protected function beforeSave(){

        

        $this->created_on = date('d-m-Y H:i:s');

        $this->last_active = date('d-m-Y H:i:s');

        echo $this->created_on;

     }

when i fill the form first it says dob can’t b blank, but it is filled with the date. Help

You need to do this:




<div class="row">

     <?php echo $form->labelEx($model,'dob'); ?>

     <?php   $this->widget('zii.widgets.jui.CJuiDatePicker', array(

    

    'attribute'=>'dob', //<------- instead of name, put attribute

    'model'=>$model,

    'value' => $model->dob, // <------- initial value

    // additional javascript options for the date picker plugin

    'options'=>array(

        'showAnim'=>'fold',

        'dateFormat'=>'dd/mm/yy',

    ),

    'htmlOptions'=>array(

        'style'=>'height:20px;'

    ),

));

?>



You can also set the name like this: ModelName[attribute] and it will work too.Please see the example on Yiiplayground: http://www.yiiplayground.cubedwater.com/index.php?r=UiModule/jui/ziiDatePicker#simpleCalendar

And for the other question:




protected function beforeSave(){

        if(parent::beforeSave()){

                $this->created_on = date('d-m-Y H:i:s');

                $this->last_active = date('d-m-Y H:i:s');

                return true;

        }

        return false;

}



thanks a lot it worked but there was no date or time being stored on the database had to change the format


$this->created_on = date('d-m-Y H:i:s');

            $this->last_active = date('d-m-Y H:i:s');


$this->created_on = date('Y-m-d H:i:s');

            $this->last_active = date('Y-m-d H:i:s');

one more thing i visited the yiiplayground but can’t find how to set the number of years, as this field is for DOB the years shown up in the calender are from 2001 to 2021 any idea on how to expand it?

It expands automatically when you try to select other years that doesnt appear on the list.

Ps: in order to avoid problems with datetime fields, i recommend you to use fields of type INT.

strtotime to save and Yii::app()->format->formatDate($model->dob) to display.