Bianca
(Biancajsen)
June 6, 2012, 7:40pm
1
Hi,
I wish to use the date format : day month year (06-06-2012) throughout my application. Can anyone tell me if there is a way to set the default date format to this format? If not, I have tried the following in a view file. Everything works fine until I save the form. Thereafter, it returns 01-01-1970 for any date I input.Here is the code:
<?php echo $form->labelEx($model,'leaveDate').' '; ?>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name' => CHtml::activeName($model, 'leaveDate'),
'model'=>$model,
'value' => date('d-m-Y',strtotime($model->leaveDate)),
'options'=>array(
'showAnim'=>'fold',
'dateFormat' => 'dd-mm-yy',
),
)); ?>
Thanks
ragua
(Brice)
June 7, 2012, 7:27am
2
Hi,
i think you must use attribute for saving your field.
When you’re dealing with date, it seems easier to use afterFind and beforeSave in the model.
Example:
protected function afterFind ()
{
if($this->class_date <> '')
{
// mise en forme de class_date
list($y, $m, $d) = explode('-', $this->class_date);
$mk=mktime(0, 0, 0, $m, $d, $y);
$this->class_date = date ('d-m-Y', $mk);
}
return parent::afterFind ();
}
protected function beforeSave ()
{
if($this->class_date <> '')
{
// mise en forme de class_date
list($d, $m, $y) = explode('-', $this->class_date);
$mk=mktime(0, 0, 0, $m, $d, $y);
$this->class_date = date ('Y-m-d', $mk);
}
return parent::beforeSave ();
}
before saving it change the format
like this
$model->admission_date=date(‘Y-m-d’,strtotime($model->admission_date));
Bianca
(Biancajsen)
June 7, 2012, 8:53am
4
ragua:
Hi,
i think you must use attribute for saving your field.
When you’re dealing with date, it seems easier to use afterFind and beforeSave in the model.
Example:
protected function afterFind ()
{
if($this->class_date <> '')
{
// mise en forme de class_date
list($y, $m, $d) = explode('-', $this->class_date);
$mk=mktime(0, 0, 0, $m, $d, $y);
$this->class_date = date ('d-m-Y', $mk);
}
return parent::afterFind ();
}
protected function beforeSave ()
{
if($this->class_date <> '')
{
// mise en forme de class_date
list($d, $m, $y) = explode('-', $this->class_date);
$mk=mktime(0, 0, 0, $m, $d, $y);
$this->class_date = date ('Y-m-d', $mk);
}
return parent::beforeSave ();
}
Merci beaucoup Ragua,
J’ai modifié le code du modèle et j’ai inclus les deux fonctions que vous m’avez données: c’est GENIAL!! Ca marche! Magnifique!! Merci encore
Bianca
(Biancajsen)
June 7, 2012, 8:54am
5
before saving it change the format
like this
$model->admission_date=date(‘Y-m-d’,strtotime($model->admission_date));
Thanks Rajith R,
You are right! Before saving, the format needs to be changed. I have implemented it as Ragua suggested. It works! Superb. Thanks for your prompt help!