This extension is a behavior that can be used in models to allow them to automatically parse and format i18N date formats.
The behavior scans for date and datetime fields in the model attributes, and do the conversions needed.
protected/extensionsIn you model, add the following code:
public function behaviors()
{
return array('datetimeI18NBehavior' => array('class' => 'ext.DateTimeI18NBehavior')); // 'ext' is in Yii 1.0.8 version. For early versions, use 'application.extensions' instead.
}
IMPORTANT! This behavior changes afterFind and beforeSave events. If your model have to have coding, please, don't forget to add the parent reference:
protected function beforeSave(){
if (!parent::beforeSave()) return false;
....your code
}
The same for afterFind method. Otherwise, this behavior will not make effect.
Note: This extension was made for MySQL. But, it can be easily changed to be useful to other databases.
Note2: Actually, the extension have problems with datetime fields, just working with date fields (FIXED)
Use version 1.1 if you are using Yii 1.0.9. If not, use the original version (with the bug described on note #2)
Fixed issue with optional date/datetime fields and incorrect convertion on datetime fields (works only with Yii 1.0.9. If you use this version, download I18N-datetime-behavior 1.1. If not, use the original version)
Total 20 comments
Extension Does not work with Latest Yii i.e 1.1.13 version.
Always show Dec 31, 1969 09:00:00
Please help
Hi All,
I've got an simple app that uses Brazilian Portuguese and English US languages i18n. In Brazilian Portuguese it works 100% fine, but whenever I've moved (following that link http://www.yiiframework.com/wiki/208/how-to-use-an-application-behavior-to-maintain-runtime-configuration/) to English US (I've notice that 'medium' datetime format is 'Dec 29, 2012 1:59:38 PM') the app does not update any date nor datetime fields (its become null and its will set to 'Dec 31, 1969 9:00:00 PM').
Any ideas, I will appreciate.
I realized modifications to work in mysql and postgresql
remembering to add in main.php
Thanks Freezy! Your code works well!
@Author: Please update this extention to include these and other improvements/issues.
Very handy.
We have a problem when using:
The date is always recorded as 1969-12-31 21:00:00
You must include the following checks:
After playing with JuiDatePicker widget, I think this behavior should be extend with more flexible date time parser. I use jQuery UI language option in the Datepicker, and after attach the behavior, it can not parse the date time format, because it is hard coded with 'medium' format ship with Yii. Besides, I use MySQL db all the time, so the 4 props $dateOutcomeFormat... are keep intact (yeah, have to read for a while to know this... At first, I think this is the input and output for the end user, not the DB server ) Anyway, this is my patch to the Behavior:
Thank for your extension. I'd like to use build-in Dateformat and Timeformat in message/locale folder that Yii provide. This extension only provide 'medium' as only format in the output. I have to add 2 more public attribute to use long or short ones.
Also, if someone uses CTimestampBehavior, this extension will also currupt the fields. To fix it you need to add
to the cycle in afterFind/afterSave/beforeSave events.
There is a bug in this extension.
If the model will be saved twice, then the datetime columns will be corrupted.
Example:
To fix this you should override not only beforeSave event in this extension, but also afterSave to take all those changes in beforeSave back.
So, you need to add afterSave method and do the same code as in afterFind.
I've spent several hours to figure out what's wrong and now I'm out of estimation on my project! First I thought, that the problem is in CAdvancedArBehavior, but then I figured out that there is a bug in this extension.
It will also be very useful, if you add public methods to convert date to timestamp and to database format (to be able to build proper datetime conditions for DB queries)
Hi all,
I have some timestamp fields in my database which are automatically populated when the record is created or modified.
Is it possible to modify use this extension to do a conversion on this type of field too, so that the presentation matches the datetime fields?
Да простят меня боги за английский язык! :) There is fix for CForm ;)
Following the Yii Blog Tutorial using yii-1.1.4, I created created the Post model along with the Post CRUD for tbl_post. I did however made a modification in the tbl_post table prior to running Gii. I changed the create_time to datetime. I was wondering if perhaps someone could help one out by showing exactly the file modifications needed to get this extension working for the blog example. So far what I've done is I've extracted the one (DateTimeI18NBehavior.php) file found within DateTimeI18NBehavior1.1.zip and placed it in the webroot:blog/protected/extensions folder. Lastly the only other changed I made was in the webroot:/blog/protected/views/post/_form.php where I added CJuiDatePicker to the create_time field. Here is what the Post model and Post-view-_form looks like:
/****START of webroot:/blog/models/Post.php*****/
/****END of webroot:/blog/protected/models/Post.php*****/
/****START of webroot:/blog/protected/views/post/_form*****/
/****END of webroot:/blog/protected/views/post/_form*****/
Works pretty good!
You can also validate the date in the right format with the type validator. Just add a rule like:
Using this extension I don't need to use CDateFormatter in every view. And dont need to reconvert again before save!
Thanks!
It works like a charm!!
Thanks, Ricardo!!
Good work
I just downloaded and opened it.
looks like all extention zip files are corrupt so .. 5 stars to kiss and make up.
zip files seem to be corrupt
Leave a comment
Please login to leave your comment.