This displays it as 1980/05/10. It does not allow us to change the format in to something friendlier, such as ‘10th May 1980’.
I know I can use ‘value’ attribute in conjunction with native date() function, however it would be nice if we could just specify the date format as an additional parameter in the code and let CFormatter do all the work.
Is this at all possible or is it worth submitting this as a feature request?
Thanks Argent - that would probably work but I don’t really want a global override - I prefer an option where I can specify the format in the widget itself.
I think I will submit this as a feature request and see what happens.
The CFormatter cannot be parameterized by widget - unfortunately - but here you can find an YFormatter workaround: Limit a CGridView field to some preset length
I think something like this will work:
<?php
class YFormatter extends CFormatter {
/**
* Override the default format function to allow paramters to the formatter.
*
* (non-PHPdoc)
* @see CFormatter::format()
*/
public function format($value, $type) {
$params = null;
if (is_array($type)) {
$params = $type;
$type = $type['type'];
}
$method = 'format' . $type;
if (method_exists($this, $method)) {
if ($params === null) {
return $this->$method($value);
} else {
return $this->$method($value, $params);
}
} else {
throw new CException(Yii::t('yii', 'Unknown type "{type}".', array(
'{type}' => $type
)));
}
}
/** Added '$params' */
public function formatDate($value, $params = array()) {
if (isset($params['dateFormat']))
return date($params['dateFormat'], $this->normalizeDateValue($value));
else
return parent::formatDate($value);
}
}