Date Format In Cdetailview

Suppose I have a date stored in a DATE column in MySQL table: 1980-05-10

Now in CDetailView we can tell it to display it in date format as follows:


array(

	'name'=>'date_of_birth',

	'type'=>'date',

),

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?

Hi

This is one way of doing…

protected function afterFind ()

{


	if($this->approv_date != null && stripos($this->approv_date, '-')) {


        list($y, $m, $d) = explode('-', $this->approv_date);


        $mk=mktime(0, 0, 0, $m, $d, $y);


        $this->approv_date = date ('d.m.Y', $mk); /// specify your format


	}


    return parent::afterFind ();


}





protected function beforesave ()


{


	if($this->approv_date != null && stripos($this->approv_date, '.')) {


        list($d, $m, $y) = explode('.', $this->approv_date);


        $mk=mktime(0, 0, 0, $m, $d, $y);


        $this->approv_date = date ('Y-m-d', $mk);


	}


    return parent::beforesave ();


}

The type=>date is call CFormatter::formatDate() - in the config you can override the default dateFormat:




//@ protected/config/main.php


return array(

  // ...

  'components' => array(

    // ...

    'formatter' => array(

      'class' => 'CFormatter',

      'dateFormat' => 'jS M Y'

    )

    // ...

  )

  // ... 

);



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);

  }

}



and replace the




array(

        'name'=>'date_of_birth',

        'type'=>'date',

),



to




array(

        'name'=>'date_of_birth',

        'type'=>array(

                'type' => 'date',

                'dateFormat' => 'jS M Y'

        )

),