Contact Us

Validating the cleaned output of HtmlHelper::dateTimeOptionTag()

Posted on 17/10/06 by Felix Geisendörfer

Deprecated post

The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.

Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.

Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).

Ok, let's make this short and painless. I just needed to validate the (cleaned) output of an HtmlHelper::dateTimeOptionTag() to be a correct datetime value matching this format:

2006-12-31 23:16:12

But for some reason I couldn't find a build-in Validator for it, so I had to to write my own:

'/^[12][0-9]{3}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]) ([01][0-9]|[2][0-4]):([0-5][0-9]|[0-9]):([0-5][0-9]|[0-9])$/'

Long live the power of regular expressions!

Here is an example how you would use this in a Model:

class Event extends AppModel
    var $name = "Event";
    var $validate = array('title' => VALID_NOT_EMPTY,
                          'start' => '/^[12][0-9]{3}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]) ([01][0-9]|[2][0-4]):([0-5][0-9]|[0-9]):([0-5][0-9]|[0-9])$/');  

I'm surprised no one had this problem before, or am I missing something ; )?

--Felix Geisendörfer aka the_undefined


You can skip to the end and add a comment.

Pierre  said on Oct 17, 2006:

There is one problem though: your regex doesn't prevent impossible data like 2006-02-31.

I wrote this function (which I put in app_model.php):

function isDate($date)

if ($date && preg_match('/^\d{4}-\d?\d-\d?\d$/', $date)) // match YYYY-MM-DD


$date = explode('-',$date);

return checkdate($date[1], $date[2], $date[0]);




return false;



You can easily extend it to add a time validator.

Felix Geisendörfer said on Oct 17, 2006:

Pierre: I agree, however, since I don't expect the Admin of the web site I'm building to create his own post with wrong information I think it's safe to ignore those cases ; ). said on Oct 17, 2006:

Felix Geisendorfer's Blog: Validating the cleaned output of HtmlHelper::dateTimeOptionTag()...


TheIdeaMan said on Oct 18, 2006:


I use a JavaScript Calender input on plain text fields for my dates instead of the dataTimeOptionTag() helper. Do you know of a way to format dates coming in and out of the database? I'm attempting to use afterFind() and beforeSave(), but that's proven to be a nightmare as I have to take every potential return from find() into account. What I'd really like is a set of getters and setters for each field in my model. Does CakePHP have anything like that? Do you know if it's in the works?

Thanks for all your great posting, Felix,

Felix Geisendörfer said on Oct 19, 2006:

TheIdeaMan: If I was you I would create custom cleanUpFields() method in your Controller (don't forget to call parent::cleanUpFields($model) in it), and format your date in there. So in all Controller functions that save a date from your JS calendar, this function would need to be called upfront.

how said on Jun 13, 2007:

YYYY-MM-DD (without HH:MM:SS) is also acceptable.
So it's better to use

'dat' => '/^[12][0-9]{3}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])( ([01][0-9]|[2][0-4]):([0-5][0-9]|[0-9])(:([0-5][0-9]|[0-9]))?)?$/',

This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.