How to change the date format? (YII)

I am using the YII structure ... and I am showing my date using the following code:

<?php echo $classified->created_date ?> 

The problem is that she shows it like this: 2012-11-04 09:34:03

How can I delete the time and just show the date?

And perhaps, if possible, show it like this: January 20, 2012 (with a month reduction)

+4
source share
5 answers

You can use the Yii CFormatter class:

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

You can extend CFormatter into your own class and then format the date, however you want, you just need to include your new component class in the main configuration file.

Another option is to use the DateFormatter parser:

 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. The only reason for this is that if you decide to change the formats, you may need to change many different types:

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

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

+1
source

Use php date function. date (format, epoch_timestamp)

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

0
source

Of course you can. You should use this code below:

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

which change the formatting of the default settings 2012-11-04 09:34:03 of January 20, 2012

link to doc / api:

format

0
source

This was my solution for the date problem, the date formatter does not 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> 
0
source

Source: https://habr.com/ru/post/1444677/


All Articles