php 如何更改日期格式?(二)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13279482/
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-25 05:06:06  来源:igfitidea点击:

How do I change the date format? (YII)

phpdateyiiformat

提问by Debrah

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 日(月份缩短)

回答by Tim Withers

You can use Yii's CFormatterclass:

你可以使用 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. The only draw back to this is that if you decide to change formats, you may have to change many different views:

有很多方法可以做到这一点...如果你愿意,你可以使用标准的 PHP。唯一的缺点是,如果您决定更改格式,您可能必须更改许多不同的视图:

echo date("m/d/Y",strtotime($classified->create_date));

回答by Midoo Simo

Yii::app()->dateFormatter->formatDateTime($classified->created_date, 'medium', false);

more examples for date format look here : http://www.yiiplayground.com/index.php?r=InternationalizationModule/datetime/basic

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

回答by tiranox85

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>

回答by Paul

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-11-04 09:34:03 on 20 Jan 2012

link to doc/api:

链接到文档/api:

format

格式

回答by user1731782

Use the php date function. date(format, epoch_timestamp)

使用 php 日期函数。日期(格式,纪元时间戳)

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

见这里http://php.net/manual/en/function.date.php