Model Rule For Dates

I am trying to find out a way of detecting any date when a model is saved. I want to manipulate the dates before they are saved. Each model has one or more date fields. Rather than going back and renaming them all to have something like FIELD-date where I can detect the "-date" in the field name, I thought I would try setting the model rule for the fields to a type of "date". But whenever I do this I always get an error when validating the data of "the format of FIELD is invalid". I tried MM/DD/YYYY, YYYY-MM-DD, MM/DD/YYYY HH:MM, and others. All return the same thing.

  1. how do I get the model field type of date to work?

  2. once I do this, will it set the data type of the variable for that field in the model to datetime or some other data type that can be checked?

  3. if neither of the above pan out, is there some other recommendation for determining a date/datetime field when submitted?


You can specify a format for the date validator in your rules:

 [['birth_dt'], 'date', 'format' => 'php:Y-m-d'],

This is especially helpful if you are storing to something like a MySQL database where your column is a date datatype. If you want to save and display in different formats, I would suggest the kartik-v datecontrol extension:

Specify things once in your config and you’ve got consistency throughout your application.