Yii Framework Forum: Date formats, i18n and CJuiDatePicker - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Date formats, i18n and CJuiDatePicker Rate Topic: ***** 1 Votes

#1 User is offline   Michael Hamann 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 29-December 09
  • Location:Karlsruhe, Germany

Posted 29 December 2009 - 08:04 AM

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
0

#2 User is offline   Maxximus 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 146
  • Joined: 24-November 08
  • Location:Spain

Posted 31 December 2009 - 10:49 AM

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).
0

#3 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,871
  • Joined: 04-October 08
  • Location:DC, USA

Posted 31 December 2009 - 01:27 PM

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.
0

#4 User is offline   Michael Hamann 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 29-December 09
  • Location:Karlsruhe, Germany

Posted 02 January 2010 - 05:41 PM

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?
0

#5 User is offline   Michael Hamann 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 29-December 09
  • Location:Karlsruhe, Germany

Posted 10 January 2010 - 08:08 AM

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.c...es/detail?id=22. With that patch it is possible to use a localized date format directly as value/attribute.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users