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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6811706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:20:35  来源:igfitidea点击:

YII : How to Change datetime format displayed on the View

phpdatetimeviewmodelyii

提问by Calua

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!

谢谢!

回答by

If you want the date stored in one format, but displayed in another format (i.e. 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.
    • beforeValidate and afterValidate: these are invoked before and after validation is performed.
    • beforeSave and afterSave: these are invoked before and after saving an AR instance.
    • beforeDelete and afterDelete: these are invoked before and after an AR instance is deleted.
    • afterConstruct: this is invoked for every AR instance created using the new operator.
    • beforeFind: this is invoked before an AR finder is used to perform a query (e.g. find(), findAll()).
    • afterFind: this is invoked after every AR instance created as a result of query.
  1. 自定义 CActiveRecord 提供了一些占位符方法,可以在子类中覆盖这些方法以自定义其工作流程。
    • beforeValidate 和 afterValidate:在执行验证之前和之后调用它们。
    • beforeSave 和 afterSave:它们在保存 AR 实例之前和之后调用。
    • beforeDelete 和 afterDelete:在删除 AR 实例之前和之后调用它们。
    • afterConstruct:为使用 new 运算符创建的每个 AR 实例调用此方法。
    • beforeFind:这是在使用 AR 查找器执行查询(例如 find()、findAll())之前调用的。
    • afterFind:在作为查询结果创建的每个 AR 实例之后调用。

回答by Lupo

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

回答by Nemoden

All you need is to use CDateFormatterand it's method format

所有你需要的是使用CDateFormatter它的方法format

OR you could use native PHP's way to format date using function date

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

I think there are lots of examples in PHPmanual 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,我可以给出你一些例子。