DateTime Validation

I searched the forum and could not get much help on validating a datetime field. Also it seems that there is no datetime validation built in to Yii…

Basically all I want to do is validate the datetime input on a form. I am using the extension ‘timepicker’ (, with this I can pick a date and time and this populates the field.

Ideally I would like the datetime validated in the format: [30-01-2010 14:00] but before being saved it will need to get converted back to MYSQL default datetime format.

Anyone able to assist? Thanks.

There is not a validator for date.

You can do like that:


 * Declares the validation rules.


public function rules()


	return array(

		// name, email, subject and body are required




public function custom()


	if !(checkdate(date('m', strtotime($this->date)), date('d', strtotime($this->date)), date('y', strtotime($this->date)))

		$this->addError('date', 'Date not valid');


Or some other way for test the date.

So what’s this then:

? ;)

Now ideally want the date to be validated in the format: [30-01-2010 14:00]

So basically I did this:

array('event_date', 'type', 'type'=>'datetime', 'datetimeFormat'=>'dd-MM-yyyy hh:mm'),

But upon saving the record I get zero values in my database; this is because the data does not conform to the MYSQL datetime format (YYYY-MM-DD HH:MM:SS).

So how can I make it convert my custom format to the MYSQL format before save?

check this thread