Yii 中的日期选择器将数据存储为 yy MM d 格式

发布于 2025-01-06 22:57:37 字数 1072 浏览 1 评论 0原文

我有一个表单,其中的输入字段如下所示,

  <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 值。没有存储其他日期值。有人可以告诉我我哪里做错了吗?任何帮助和建议都将非常有用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

铃予 2025-01-13 22:57:37

试试这个:

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

将上面的代码添加到您的模型中。它应该有效。

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.

深海夜未眠 2025-01-13 22:57:37

我对欧洲日期也有类似的问题,格式如下:“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)));

希望这有帮助。

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.

高跟鞋的旋律 2025-01-13 22:57:37

这对我有用:

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

this worked for me:

public function beforeSave()
{
    $this->due_date = date('Y-m-d', strtotime( $this->due_date));
    return parent::beforeSave();
}
温柔一刀 2025-01-13 22:57:37
<?= $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'

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

            ]
        ]
    ]);?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文