Date formats, i18n and CJuiDatePicker

Hi!

I’m quite new to Yii and so far I’m quite impressed. I was also impressed when I saw how easy it is to integrate things like a cool-looking Datepicker. But then the problems began. I’m using Yii 1.1 RC.

I’ve localized my application and thus my date attributes are in the form “dd.mm.yyyy” (like 29.12.2009). Now I’m not quite sure where the conversion from a date as it is stored in the database (SQlite seems to accept even that German date format) to a date in the form “dd.mm.yyyy” should occur. I thought CActiveRecord might do that, but apparently there are still things I don’t really understand and that might even be a bug. First of all I expect the date the user enters to be in the form “dd.mm.yyyy”. That’s why I’ve also set a validation rule as follows:


array('begin, end', 'type', 'type' => 'date', 'dateFormat' => Yii::app()->getLocale()->getDateFormat())

. And this is also the format the CJuiDatePicker produces when I set it’s language to “de”. But then that’s of course also the format that is again put into input as value. But whenever a date in the format “dd.mm.yyyy” is set as value in the input form, the date picker transforms it into the current date during initialization. When I manually enter the date in the format “2009/12/24”, the date picker takes that date and transforms it into the German date format during initialization. So either the date format is set too late (e.g. only after the initialization has already taken place) or I haven’t understood how Yii expects me to handle different date formats (as I also rather want to have a date in a form like 2009-12-29 in the model in order to be able to e.g. sort it properly).

Greetings

Michael

Had the same issue with CJuiDatePicker, and decided to feed it like this for now:


'value'=>CTimestamp::formatDate('m/d/Y',$item->validFrom),

I save date as timestamp, so I can decide the format later on (multilang app).

You can define a new property named validFromText in the model as follows,




function getValidFromText()

{

    return date('m/d/Y', $this->validFrom);

}


function setValidFromText($value)

{

   $this->validFrom=strtotime($value);

}



Then, use validFromText to collect user input (and validation) instead of validFrom.

Thank you for that tip, that’s excellent, but however it doesn’t solve the problem with the localized CJuiDatePicker that needs another date format as input than the one it displays and generates. I’ve solved this now by manually specifying the name and the value, but imho that’s quite ugly and I’d love to be able to specify it with model and attribute. Should I report this to the bug tracker or is this the desired behaviour of CJuiDatePicker?

I’ve added a but report to the bug tracker including a patch that resolves the problem of the date picker initialization, see http://code.google.com/p/zii/issues/detail?id=22. With that patch it is possible to use a localized date format directly as value/attribute.