Datepicker in Yii is storing data as yy MM d format

16,180

Solution 1

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.

Solution 2

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

In 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 'medium' locale format fits my validation needs.

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 the conversion back to MySQL format, for save, date comparisons...:

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

Hope this helps.

Share:
16,180
NewUser
Author by

NewUser

I am just a beginner in the field of web. I like to know about new technology behind Web Sites and Web Applications.

Updated on July 15, 2022

Comments

  • NewUser
    NewUser almost 2 years

    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.

  • bool.dev
    bool.dev about 12 years
    Let me know if any clarification is required.
  • bool.dev
    bool.dev about 12 years
    alright glad it worked, do remember to call parent methods, when you are overriding them, so that events are properly raised. also the afterFind helps us to reformat the data in the way we wanted, obviously you could have done that in the view itself, but i thought this is better.
  • NewUser
    NewUser about 12 years
    ok..Can you tell me what is the role of parent here?Why it is called here?
  • bool.dev
    bool.dev about 12 years
    the parent methods raise events onAfterFind in the parent afterFind() and onBeforeSave in the parent beforeSave(), read the documentation for the methods to know more, you should also check out the source of these parent methods in the yii documentation.
  • bool.dev
    bool.dev about 12 years
    please note that i have changed the code, the function should be protected and not public. this is important.
  • Md.Sukel Ali
    Md.Sukel Ali about 5 years
    Try to add some description of your code. Why your code work ?