Date & Time Formatting

In mySQL:




Table: News

Field: datestamp

Field Type: date



In View:




<?php foreach($news as $row): ?>

<h3 class="h3_2"><?php echo $row->datestamp ?></h3>



Returns "2009-11-22"

I would like to format as "11/22/2009". What is the best way to accomplish this?

I can use


<?php echo Yii::app()->dateFormatter->formatDateTime($row->datestamp, 'long', '') ?>

but the format is not quite what I want and seems quite long to put in the view.

I tried to use CDateFormatter::format(‘mm/dd/yyyy’, $row->datestamp) but get a different error.

1. Should I or can I do this at the "News" activerecord model level?

2. Why is there not an easier formatting function since this is a common need?

Thanks!

TL

Have you set your language on ‘config/main.php’?

Inside there, put this config:

‘language’ => ‘your-language’,

‘sourceLanguage’ => ‘your-language’

In your language configuration, your date format should be ‘mm/dd/yyyy’

Besides, the command to output the language is this format (medium), should be


<?php echo Yii::app()->dateFormatter->formatDateTime($row->datestamp, 'medium', null) ?>

I would’ve used straightforward date(‘Y-m-d’, $row->datestamp). Always predictable.