Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the date format? (YII)

Tags:

date

php

format

yii

I'm using the YII framework... and I am displaying my date using the following code:

<?php echo $classified->created_date ?>

Problem is that it shows it like this: 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)

like image 866
Debrah Avatar asked Feb 18 '23 14:02

Debrah


2 Answers

You can use Yii's 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 would like, you would just need to include your new component class in the main config file.

Another option is using Yii's date parser and 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:

echo date("m/d/Y",strtotime($classified->create_date));
like image 65
Tim Withers Avatar answered Feb 21 '23 04:02

Tim Withers


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

like image 36
Midoo Simo Avatar answered Feb 21 '23 04:02

Midoo Simo