labelEx($mo...">

Yii Datepicker stores data in yy MM d format

I have a form in which the input box looks like this

<div class="row"> <?php echo $form->labelEx($model,'due_date'); ?> <?php $this->widget('zii.widgets.jui.CJuiDatePicker', array( 'attribute'=>'due_date', 'model'=>$model, 'options' => array( 'mode'=>'focus', 'dateFormat'=>'d MM, yy', 'showAnim' => 'slideDown', ), 'htmlOptions'=>array('size'=>30,'class'=>'date'), ) ); ?> <?php echo $form->error($model,'due_date'); ?> </div> 

I made saving this form in a model file. This is something like this.

  protected function beforeSave() { $this->due_date=date('Ym-d', strtotime(str_replace(",", "", $this->due_date))); return TRUE; } 

CJuiDatePicker is used to save data from a date picker. It shows a date in d mm yy format during the save, but when I am going to update the form specified in yy MM d format. If I change the date format beforeSave (), it saves the date format in 0000-00-00. Other date values ​​are not saved. Can someone tell me where I am doing wrong? Any help and suggestions would be very helpful.

+6
source share
3 answers

Try the following:

 protected function afterFind(){ parent::afterFind(); $this->due_date=date('d F, Y', strtotime(str_replace("-", "", $this->due_date))); } protected function beforeSave(){ if(parent::beforeSave()){ $this->due_date=date('Ym-d', strtotime(str_replace(",", "", $this->due_date))); return TRUE; } else return false; } 

Add the above code to your model. And it should work.

+8
source

I had a similar problem with European dates, formatted as: 'dd / mm / yyyy', and this is what I use:

In the model rules:

  public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('date','date','format'=>Yii::app()->locale->getDateFormat('medium')), 

because the format of the "middle" language meets my verification requirements.

In the form, I use:

  <?php echo $form->labelEx($model,'date'); ?> <?php $this->widget('zii.widgets.jui.CJuiDatePicker', array( //'name'=>'date', 'model'=>$model, 'attribute'=>'date', 'language'=>Yii::app()->language=='es' ? 'es' : null, 'options'=>array( 'changeMonth'=>'true', 'changeYear'=>'true', 'yearRange' => '-99:+2', 'showAnim'=>'fold', // 'show' (the default), 'slideDown', 'fadeIn', 'fold' 'showOn'=>'button', // 'focus', 'button', 'both' 'dateFormat'=>'dd/mm/yy', 'value'=>date('dd/mm/yy'), 'theme'=>'redmond', 'buttonText'=>Yii::t('ui','Select form calendar'), 'buttonImage'=>Yii::app()->request->baseUrl.'/images/calendar.gif', 'buttonImageOnly'=>true, ), 'htmlOptions'=>array( 'style'=>'vertical-align:top', 'class'=>'span2', ), ));?> <?php echo $form->error($model,'date'); ?> 

And switching back to MySQL format, for saving, comparing dates ...:

 $date=strftime('%Y-%m-%d', strtotime(str_replace("/", "-", $this->date))); 

Hope this helps.

+3
source

this worked for me:

 public function beforeSave() { $this->due_date = date('Ym-d', strtotime( $this->due_date)); return parent::beforeSave(); } 
0
source

Source: https://habr.com/ru/post/908614/


All Articles