Yii CJuiDatePicker dateformat

Today I struggled with the datepicker provided by Yii. I wanted to show the date in format ’24-02-2013′. In this post I will show how I solved this.

The following snippets are all located in my model:

This will validate the user input:

public function rules()
{
	return array(
		array('matchDate', 'type', 'type'=>'date', 'dateFormat'=>'dd-MM-yyyy'),	
	);
}

The beforeSave function will change the user input back to the original format to store it in the database. The afterFind method does conversion the other way around.

protected function beforeSave()
{
	$this->matchDate = date('Y-m-d', CDateTimeParser::parse($this->matchDate, 'dd-MM-yyyy'));
	return parent::beforeSave();
}
	
protected function afterFind()
{
	$this->matchDate = Yii::app()->dateFormatter->format('dd-MM-yyyy', $this->matchDate);
	return parent::afterFind();
}

To show the widget (in my view) I used the following code:

<?php 
	$this->widget('zii.widgets.jui.CJuiDatePicker',array(
		'name'=>'matchDate',
		'language'=>'nl',
		'attribute'=>'matchDate',
		'model'=>$model,
		// additional javascript options for the date picker plugin
		'options'=>array(
		      	'showAnim'=>'fold',
			'dateFormat'=>'dd-mm-yy'
		),
		'htmlOptions'=>array(
		      	'style'=>'height:20px;'
		),
	));
?>

Note different dateFormat patterns!!

3 comments to Yii CJuiDatePicker dateformat

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>