简体   繁体   English

YII:如何更改视图上显示的日期时间格式

[英]YII : How to Change datetime format displayed on the View

A newbie question in Yii: I have a model of a table in Yii that contain a datetime field. Yii中的一个新手问题:我在Yii中有一个包含日期时间字段的表模型。

I'm using CActiveForm to display this field: 我正在使用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>

but the textfield displayed is on datetime format come from MySQL which is yyyy-mm-dd hh:mm:ss 但显示的文本字段是日期时间格式来自MySQL,即yyyy-mm-dd hh:mm:ss

How could I change the format displayed on the textfield to a different time format? 如何将文本字段上显示的格式更改为其他时间格式? (maybe dd/mm/yy or mm/dd/yy or else) (也许dd / mm / yy或mm / dd / yy或者其他)

any help would be appreciated. 任何帮助,将不胜感激。

thanks! 谢谢!

If you want the date stored in one format, but displayed in another format (ie multiple views), then consider changing it in the model. 如果您希望以一种格式存储日期,但以其他格式(即多个视图)显示,则考虑在模型中更改它。

For example: 例如:

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

Which methods you override depends on what you're trying to achieve. 您覆盖哪些方法取决于您要实现的目标。

From the docs : 来自文档

  1. Customization CActiveRecord provides a few placeholder methods that can be overridden in child classes to customize its workflow. 自定义CActiveRecord提供了一些占位符方法,可以在子类中重写这些方法以自定义其工作流。
    • beforeValidate and afterValidate: these are invoked before and after validation is performed. beforeValidate和afterValidate:在执行验证之前和之后调用它们。
    • beforeSave and afterSave: these are invoked before and after saving an AR instance. beforeSave和afterSave:在保存AR实例之前和之后调用它们。
    • beforeDelete and afterDelete: these are invoked before and after an AR instance is deleted. beforeDelete和afterDelete:在删除AR实例之前和之后调用它们。
    • afterConstruct: this is invoked for every AR instance created using the new operator. afterConstruct:为使用new运算符创建的每个AR实例调用此方法。
    • beforeFind: this is invoked before an AR finder is used to perform a query (eg find(), findAll()). beforeFind:在使用AR finder执行查询(例如find(),findAll())之前调用它。
    • afterFind: this is invoked after every AR instance created as a result of query. afterFind:在作为查询结果创建的每个AR实例之后调用。

This worked for me: 这对我有用:

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

All you need is to use CDateFormatter and it's method format 您只需要使用CDateFormatter及其方法format

OR you could use native PHP's way to format date using function date 或者您可以使用本机PHP的方式使用函数date格式化date

I think there are lots of examples in PHP manual on formatting options (as well as explanation of what each format modifier mean), so it wouldn't be a problem to figure things out, but if you have any problems with Yii's CDateFormatter , I can give you some examples. 我认为PHP手册中有很多关于格式化选项的例子(以及每个格式修饰符的含义的解释),所以解决问题不是问题,但如果你对Yii的CDateFormatter有任何问题,我可以举几个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM