【问题标题】:YII : How to Change datetime format displayed on the ViewYII:如何更改视图上显示的日期时间格式
【发布时间】:2011-10-12 07:21:48
【问题描述】:

Yii 中的一个新手问题: 我在 Yii 中有一个包含日期时间字段的表格模型。

我正在使用 CActiveForm 来显示这个字段:

<div class="row">
    <?php echo $form->labelEx($model,'createdon'); ?>
    <?php echo $form->textField($model,'createdon', array('id'=>'createdon')); ?>
    <?php echo $form->error($model,'createdon'); ?>
</div>

但显示的文本字段是日期时间格式,来自 MySQL,即 yyyy-mm-dd hh:mm:ss

如何将文本字段上显示的格式更改为不同的时间格式? (可能是 dd/mm/yy 或 mm/dd/yy 或其他)

任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: php datetime view model yii


    【解决方案1】:

    这对我有用:

    class SomeClass extends CActiveRecord
    {
    
        protected function afterFind()
        {
            // convert to display format
            $this->my_date_time = DateTime::createFromFormat('Y-m-d H:i:s', $this->my_date_time)->format('d-m-Y');
    
            parent::afterFind();
        }
    
        protected function beforeSave()
        {
            // convert to storage format
            $this->my_date_time = DateTime::createFromFormat('d-m-Y', $this->my_date_time)->format('Y-m-d H:i:s');
    
            return parent::beforeSave();
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果您希望日期以一种格式存储,但以另一种格式显示(即多个视图),请考虑在模型中更改它。

      例如:

      class Whatever extends CActiveRecord    
      {
          protected function afterFind ()
          {
                  // convert to display format
              $this->createdon = strtotime ($this->createdon);
              $this->createdon = date ('m/d/Y', $this->createdon);
      
              parent::afterFind ();
          }
      
          protected function beforeValidate ()
          {
                  // convert to storage format
              $this->createdon = strtotime ($this->createdon);
              $this->createdon = date ('Y-m-d', $this->createdon);
      
              return parent::beforeValidate ();
          }
      }
      

      您覆盖哪些方法取决于您要实现的目标。

      From the docs

      1. 自定义 CActiveRecord 提供了一些占位符方法 可以在子类中重写以自定义其工作流程。
        • beforeValidate 和 afterValidate:它们在之前和之后调用 进行验证。
        • beforeSave 和 afterSave:这些被调用 保存 AR 实例之前和之后。
        • beforeDelete 和 afterDelete: 这些在 AR 实例被删除之前和之后被调用。
        • afterConstruct:为使用创建的每个 AR 实例调用 新的运营商。
        • beforeFind:在 AR finder 之前调用 用于执行查询(例如 find()、findAll())。
        • afterFind:这是 在每个作为查询结果创建的 AR 实例之后调用。

      【讨论】:

      • 嘿...谢谢,afterFind() 方法是我正在寻找的方法 :)
      【解决方案3】:

      您只需要使用CDateFormatter 及其方法format

      或者您可以使用原生 PHP 的方式使用函数 date 格式化日期

      我认为PHP 手册中有很多关于格式选项的示例(以及对每个格式修饰符含义的解释),所以弄清楚事情不是问题,但如果你有任何问题有 Yii 的CDateFormatter,我可以举几个例子。

      【讨论】:

      • 在将值显示在表单上之前,我应该在哪里格式化?我可以使用这样的东西吗: textField($model,date('d-m-Y','createdon'), array('id'=>'createdon')); ?> ??或者在 yii 模型上有什么我可以做到的事件或行为?
      • 基本上你希望你的日期由于语言环境而被格式化(你可以使用CLocale设置语言环境)。在我看来,我通过了CDateFormatter——这并没有让我很困扰。如果您的项目针对您所在地区进行了本地化,并且您不考虑将其本地化到其他地区,您可以使用date('d-m-Y',&lt;timestamp&gt;)(Yii 只是为您提供了一个工具 - 使用与否是您的选择)。否则将 [CDateFormatter] 与 [CLocale] 结合使用会更方便。
      • "或者在 yii 模型上有什么事件或行为我可以做到吗?"。我没有,但我认为你可以附加一个行为。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 2018-03-27
      • 1970-01-01
      • 2022-08-19
      • 2020-12-22
      • 1970-01-01
      相关资源
      最近更新 更多