CJuiDatePicker

How to change the CJuiDatePicker date format as dd-mm-yyyy and i want to convert it to store in database as mm-dd-yyyy

I have found my self and fix with the following

_form.php

==========

$this->widget(‘zii.widgets.jui.CJuiDatePicker’, array(

                'model'=>$model,


                'attribute'=>'dob',


                // additional javascript options for the date picker plugin


                'options' => array(


                    'showAnim' => 'fold',


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


                ),


                'htmlOptions' => array(


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


                ),


            ));

model

=====

protected function beforeSave()

    {            


        $this->dob=date('Y-m-d',  strtotime($this->dob));


        return TRUE;


    }

to show on updation time create the function in model

======================================================

protected function afterFind()

    {


        $this->dob=date('d-m-Y',  strtotime($this->dob));            


        return TRUE;


    }

that may be done using purely CJuiDatePicker:

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


            'model' => $model,


            'attribute' => 'dob',


            'options' => array(


                'showAnim' => 'fold',


                'dateFormat' => 'mm-dd-yy', // save to db format


                'altField' => '#self_pointing_id',


                'altFormat' => 'dd-mm-yy', // show to user format


            ),


            'htmlOptions' => array(


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


            ),


        ));

However you still may want to Validate input server side.

It works fine for the first time with simple change if we changes it does not work again(means it show wrong year)

$this->widget(‘zii.widgets.jui.CJuiDatePicker’, array(

            'model' => $model,


            'attribute' => 'dob',


            'options' => array(


                'showAnim' => 'fold',


                'dateFormat' => 'yy-mm-dd', // save to db format


                'altField' => '#dob_id',


                'altFormat' => 'dd-mm-yy', // show to user format


            ),


            'htmlOptions' => array(


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


                'id'=>'dob_id',    


            ),


        ));

thanks Thirumalai, this worked perfectly for me

Think you should rise the parent method instead:


return parent::beforeSave()

Also, I should advice to use mktime to generate time intead of strtotime, specially on non english dates.


list($d, $m, $y) = explode('/', $dob1);

$mk=mktime(0, 0, 0, $m, $d, $y);



Thanks proto for sharing this tip about the date format. It was really useful to me.

I also found out that in my project it was necessary to specify the name as follows:




<div class="row">

  <?php echo $form->labelEx($model,'Date Of Birth'); ?>

  <?php 

   $form->widget('zii.widgets.jui.CJuiDatePicker', array(

  	'model'=>$model,

  	'attribute'=>'dateofbirth',

  	'name'=>$model->dateofbirth,	// This is how it works for me.

  	'value'=>$model->dateofbirth,

  	'options'=>array('dateFormat'=>'yy-mm-dd', 

               		'altFormat'=>'dd-mm-yy', 

               		'changeMonth'=>'true', 

               		'changeYear'=>'true', 

               		'yearRange'=>'1920:2010', 

               		'showOn'=>'both',

               		'buttonText'=>'...'),

  	'htmlOptions'=>array('size'=>'10')

   ));

  ?>

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

</div>



If I just write


'name'=>'dateofbirth'

in that line of code then the value is not saved in my database.

thank jogasa21. your answer solve my prob. XD

Hi proto

I tried your way. But without using beforeSave method mentioned above I couldnt update the changes in DatePicker box.

Could you explain how you have done this without beforeSave method?

Hi,

In my case, for I use the format "dd-mm-yy" in the frontend, but mySQL stores the value as "yy-mm-dd". Well, with your help, this code works for me.

In the view file:

[html]

&lt;?php &#036;this-&gt;widget('zii.widgets.jui.CJuiDatePicker', 


array(


'model'=&gt;&#036;model,


'attribute'=&gt;'pc_fecha_nacimiento',


'value'=&gt;&#036;model-&gt;pc_fecha_nacimiento,


'language'=&gt;'es',


'options'=&gt;array(


    	'showAnim'=&gt;'fold',


	'mode'=&gt;'datetime',


	'dateFormat' =&gt; 'dd-mm-yy', // save to db format


	'changeMonth'=&gt;'true', 


	'changeYear'=&gt;'true', 


	'yearRange'=&gt;'1900:2010', 


	'showOn'=&gt;'both',


	'buttonText'=&gt;'...',		


),


	'htmlOptions'=&gt;array('style'=&gt;'height:20px;'),


    )


    ); ?&gt;[/html]

In the model file:


    protected function afterFind ()

    {

    	list($y, $m, $d) = explode('-', $this->pc_fecha_nacimiento);

	$mk=mktime(0, 0, 0, $m, $d, $y);

	$this->pc_fecha_nacimiento = date ('d-m-Y', $mk);

    	

        return parent::afterFind ();

    }


    protected function beforeSave ()

    {

    	list($d, $m, $y) = explode('-', $this->pc_fecha_nacimiento);

	$mk=mktime(0, 0, 0, $m, $d, $y);

	$this->pc_fecha_nacimiento = date ('Y-m-d', $mk);

		

        return parent::beforeSave ();

    }	

Handling dates in the model is correct if you don’t have a multiformat site (That is, you want to accept dates from different countries - and formats)

CJuiDatePicker must create a "hidden" field when altField option is set, so JuiDatePicker can populate the other field.

Here’s an approach. It involves changing core Yii CJuiDatePicker:


...

		if ($this->flat===false)

		{

			if($this->hasModel())

                            echo CHtml::activeTextField($this->model,$this->attribute,$this->htmlOptions);

			else

                            echo CHtml::textField($name,$this->value, $this->htmlOptions);


                        if (isset($this->options['altField'])) {

                            $hHtmlOptions = array();

                            chtml::resolveNameID($this->model, $this->options['altField'], $hHtmlOptions);

                            echo CHtml::activeHiddenField($this->model,$this->options['altField'], $hHtmlOptions);

                            $attribute = $this->options['altField'];

                            $this->options['defaultDate'] = $this->model->$attribute;

                            $this->options['altField'] = '#' . $hHtmlOptions['id'];

                        }

		}

		else



Thank you jmariani for sharing your approach.

Tried all the options here , fore some strange reason none worked apart from msteel’s . . .