I’m getting confused by the different ways to specifiy a date format in Yii;
In my application I have some models witch contain (user supplied) dates.
The dates are saved as unix timestamps (integers) in the models (and DB). Off course I want to transform this timestamp into a human-readable format in the user interface.
In the user interface I want dates to be displayed and inputted as ‘31-12-2011’;
To transform a timestamp to a readable date I use
CFormatter : Yii::app()->format->formatDate( $date ); //date format is specified in config file as ‘d-m-Y’
CDateFormatter : Yii::app()->dateFormatter->formatDate( $format, $date ); // $format == ‘dd-MM-yyyy’
To transform a human readable date back into a timestamp I use:
CDateTimeParser : CDateTimeParser::parse( $date, $format ); // $format == ‘dd-MM-yyyy’
I’ve got this working now, but I struggled with it, because the way you specifie the format for a date (or datetime) is different for all three:
CFormatter uses PHP’s date() and therefore expects a format as accepted by date (specs: http://php.net/manual/en/function.date.php)
CDateFormatter uses CLDR (specs: http://www.unicode.org/reports/tr35/#Date_Format_Patterns)
CDateTimeParser uses its own pattern specification (specs: http://www.yiiframework.com/doc/api/1.1/CDateTimeParser)
(might seem like a subset of PHP’s date function specification, but its not, for example minute (00-59) is ‘mm’ in CDateTimeParser but ‘i’ in PHP’s date)
I think it would be much easier if there would be one pattern specification used by all formatting/parsing functions.
(I now have three date formats in my config file all describing the same pattern!!)
What do you think?
Has anybody got any tips to prevent confusion when using date formatting/parsing functions?