【问题标题】:Datepicker in Yii is storing data as yy MM d formatYii 中的 Datepicker 将数据存储为 yy MM d 格式
【发布时间】:2012-03-07 20:07:50
【问题描述】:

我有一个表单,其中输入字段是这样的

  <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> 

我已经将此表格保存在模型文件中。是这样的

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

CJuiDatePicker 用于保存来自日期选择器的数据。它在保存时以 d mm yy 格式显示日期,但是当我要更新表格时,日期以 yy MM d 格式显示。如果我要更改 beforeSave() 的日期格式,它正在存储日期格式为 0000-00-00 值。没有其他日期值正在存储。有人可以告诉我我在哪里做错了吗?任何帮助和建议都将非常有用。

【问题讨论】:

  • 您是否尝试在更改 $this->due_date 的值之后,在保存之前转储它?将其注销并检查那里的值是否正确。
  • 我不太明白你在描述什么。你能说得更具体点吗?
  • @Jon,我想他是说当他尝试更新模型时,日期字段以 YYYY-MM-DD 格式显示(填充)。
  • 感谢@bool.dev 的简短解释..
  • 好吧,在下面试试我的解决方案,顺便说一句!跟进 span>

标签: php mysql datepicker yii


【解决方案1】:

试试这个:

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;
}

将上述代码添加到您的模型中。它应该可以工作。

【讨论】:

  • 如果需要任何澄清,请告诉我。
  • 好吧,很高兴它成功了,记得在覆盖它们时调用parent 方法,以便正确引发事件。 afterFind 还可以帮助我们以我们想要的方式重新格式化数据,显然你可以在视图本身中做到这一点,但我认为这样会更好。
  • ok..你能告诉我这里的父母是什么角色吗?为什么在这里叫它?
  • 父方法在父 afterFind()onBeforeSave 中引发事件 onAfterFind 在父 beforeSave(),阅读文档以了解更多方法,您还应该查看源yii 文档中的这些父方法。
  • 请注意我已经更改了代码,函数应该是protected 而不是public。这很重要。
【解决方案2】:

我对欧洲日期也有类似的问题,格式如下:'dd/mm/yyyy',这就是我使用的:

在模型规则中:

    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')),

因为“中等”语言环境格式符合我的验证需求。

以我使用的形式:

            <?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'); ?>

并转换回 MySQL 格式,用于保存、日期比较...:

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

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    这对我有用:

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

    【讨论】:

      【解决方案4】:
      <?= $form->field($model, 'As_purchaseDate')->widget(
          'trntv\yii\datetime\DateTimeWidget',
          [
              'phpDatetimeFormat' => 'yyyy-MM-dd',
              'clientOptions' => [
                  'minDate' => new \yii\web\JsExpression('new Date("01-01-1996")'),
                  'allowInputToggle' => false,
                  'sideBySide' => true,
                  'widgetPositioning' => [
                     'horizontal' => 'auto',
                     'vertical' => 'auto'
      
                  ]
              ]
          ]);?>
      

      【讨论】:

      • 尝试添加一些代码描述。为什么你的代码有效?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多