简体   繁体   English

如何更改日期格式? (YII)

[英]How do I change the date format? (YII)

I'm using the YII framework... and I am displaying my date using the following code: 我正在使用YII框架......我使用以下代码显示我的日期:

<?php echo $classified->created_date ?>

Problem is that it shows it like this: 2012-11-04 09:34:03 问题是它显示如下:2012-11-04 09:34:03

How can I remove the time and just show the date? 如何删除时间并显示日期?

And maybe if possible show it like this: 20 Jan 2012 (with the month shortened) 也许如果可能的话,就像这样:2012年1月20日(月份缩短)

You can use Yii's CFormatter class: 你可以使用Yii的CFormatter类:

echo Yii::app()->format->date(strtotime($classified->created_date));

You can extend CFormatter into your own class, and then format the date however you would like, you would just need to include your new component class in the main config file. 您可以将CFormatter扩展到您自己的类中,然后根据需要格式化日期,您只需要在主配置文件中包含新的组件类。

Another option is using Yii's date parser and dateFormatter: 另一种选择是使用Yii的日期解析器和dateFormatter:

echo Yii::app()->dateFormatter->formatDateTime(CDateTimeParser::parse($classified->create_date, 'yyyy-MM-dd'),'medium',null);

There are many ways to do this... You can use standard PHP if you want to. 有很多方法可以做到这一点......如果你愿意,你可以使用标准的PHP。 The only draw back to this is that if you decide to change formats, you may have to change many different views: 唯一的回归是,如果您决定更改格式,您可能需要更改许多不同的视图:

echo date("m/d/Y",strtotime($classified->create_date));
Yii::app()->dateFormatter->formatDateTime($classified->created_date, 'medium', false);

日期格式的更多示例请看: http//www.yiiplayground.com/index.php? r = InternationalizationModule / datetime / basic

this was my solution for the date problem, the date formater wont work for me 这是我对日期问题的解决方案,日期格式化器不适合我

<div class="row">
    <?php echo $form->labelEx($model,'fecha_inicio'); ?>
    <?php //echo $form->textField($model,'fecha_inicio'); 
    $this->widget('zii.widgets.jui.CJuiDatePicker',array(
        'name'=>'Eventos[fecha_inicio]',
        // additional javascript options for the date picker plugin
        'options'=>array(
            'showAnim'=>'fold',
            'dateFormat' => 'yy-mm-dd',
            'value' => 'yy-mm-dd',
        ),
        'htmlOptions'=>array(
            'placeholder'=>'Fecha',
        ),
    ));
    ?>
    <?php echo $form->error($model,'fecha_inicio'); ?>
</div>

Use the php date function. 使用php日期函数。 date(format, epoch_timestamp) 日期(格式,epoch_timestamp)

See here http://php.net/manual/en/function.date.php http://php.net/manual/en/function.date.php

Of course you can. 当然可以。 You should use this code below: 您应该使用以下代码:

Yii::app()->dateFormatter->format("dd MMM yyyy", $classified->create_data)

which change formatting default settings form 2012-11-04 09:34:03 on 20 Jan 2012 在2012年1月20日2012-11-04 09:34:03更改格式默认设置

link to doc/api: 链接到doc / api:

format 格式

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

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