Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datepicker in Yii is storing data as yy MM d format

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.

like image 882
NewUser Avatar asked Feb 16 '12 12:02

NewUser


2 Answers

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.

like image 55
bool.dev Avatar answered Oct 07 '22 18:10

bool.dev


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.

like image 23
alvema Avatar answered Oct 07 '22 16:10

alvema