PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 5, 2022

[FIXED] Datepicker in Yii is storing data as yy MM d format

 February 05, 2022     datepicker, mysql, php, yii     No comments   

Issue

I have a form,in which the input field is 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 have made save this form in model file.It is something like this

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

CJuiDatePicker is used to save the data from Date picker. It is showing the date in d mm yy format at the time of save but when I am going to update the form the date is showing in yy MM d format.If I am changing the dateformat of beforeSave(), it is storing the date format in 0000-00-00 values.No other date values are storing. Can some one tell me where I am doing wrong? Any help and suggestions will be highly appriciable.


Solution

Try this:

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('Y-m-d', strtotime(str_replace(",", "", $this->due_date)));
        return TRUE;
    }
    else return false;
}

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



Answered By - bool.dev
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing