Yii Framework Forum: CJuiDatePicker - Yii Framework Forum

Jump to content

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

CJuiDatePicker Rate Topic: -----

#1 User is offline   Thirumalai 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 20-November 10

Posted 22 November 2010 - 12:10 AM

How to change the CJuiDatePicker date format as dd-mm-yyyy and i want to convert it to store in database as mm-dd-yyyy
0

#2 User is offline   Thirumalai 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 20-November 10

Posted 22 November 2010 - 12:39 AM

I have found my self and fix with the following
_form.php
==========
$this->widget('zii.widgets.jui.CJuiDatePicker', array(

'model'=>$model,
'attribute'=>'dob',
// additional javascript options for the date picker plugin
'options' => array(
'showAnim' => 'fold',
'dateFormat'=>'dd-mm-yy',
),
'htmlOptions' => array(
'style' => 'height:20px;'
),
));

model
=====
protected function beforeSave()
{
$this->dob=date('Y-m-d', strtotime($this->dob));
return TRUE;
}

to show on updation time create the function in model
======================================================

protected function afterFind()
{
$this->dob=date('d-m-Y', strtotime($this->dob));
return TRUE;
}
1

#3 User is offline   proto 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 02-November 10
  • Location:Switzerland

Posted 25 November 2010 - 04:26 AM

that may be done using purely CJuiDatePicker:

$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model' => $model,
'attribute' => 'dob',
'options' => array(
'showAnim' => 'fold',
'dateFormat' => 'mm-dd-yy', // save to db format
'altField' => '#self_pointing_id',
'altFormat' => 'dd-mm-yy', // show to user format
),
'htmlOptions' => array(
'style' => 'height:20px;'
),
));

However you still may want to Validate input server side.
0

#4 User is offline   Thirumalai 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 20-November 10

Posted 25 November 2010 - 09:12 AM

It works fine for the first time with simple change if we changes it does not work again(means it show wrong year)

$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model' => $model,
'attribute' => 'dob',
'options' => array(
'showAnim' => 'fold',
'dateFormat' => 'yy-mm-dd', // save to db format
'altField' => '#dob_id',
'altFormat' => 'dd-mm-yy', // show to user format
),
'htmlOptions' => array(
'style' => 'height:20px;',
'id'=>'dob_id',
),
));

View Postproto, on 25 November 2010 - 04:26 AM, said:

that may be done using purely CJuiDatePicker:

$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model' => $model,
'attribute' => 'dob',
'options' => array(
'showAnim' => 'fold',
'dateFormat' => 'mm-dd-yy', // save to db format
'altField' => '#self_pointing_id',
'altFormat' => 'dd-mm-yy', // show to user format
),
'htmlOptions' => array(
'style' => 'height:20px;'
),
));

However you still may want to Validate input server side.

0

#5 User is offline   joeyk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 28-January 11

Posted 07 February 2011 - 07:45 AM

View PostThirumalai, on 22 November 2010 - 12:39 AM, said:

I have found my self and fix with the following
_form.php
==========
$this->widget('zii.widgets.jui.CJuiDatePicker', array(

'model'=>$model,
'attribute'=>'dob',
// additional javascript options for the date picker plugin
'options' => array(
'showAnim' => 'fold',
'dateFormat'=>'dd-mm-yy',
),
'htmlOptions' => array(
'style' => 'height:20px;'
),
));

model
=====
protected function beforeSave()
{
$this->dob=date('Y-m-d', strtotime($this->dob));
return TRUE;
}

to show on updation time create the function in model
======================================================

protected function afterFind()
{
$this->dob=date('d-m-Y', strtotime($this->dob));
return TRUE;
}



thanks Thirumalai, this worked perfectly for me
0

#6 User is offline   Panurge 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 16-March 11
  • Location:Minas Gerais

Posted 19 March 2011 - 07:12 AM

View PostThirumalai, on 22 November 2010 - 12:39 AM, said:

model
=====
protected function beforeSave()
{
$this->dob=date('Y-m-d', strtotime($this->dob));
return TRUE;
}
protected function afterFind()
{
$this->dob=date('d-m-Y', strtotime($this->dob));
return TRUE;
}

Think you should rise the parent method instead:
return parent::beforeSave()

Also, I should advice to use mktime to generate time intead of strtotime, specially on non english dates.
list($d, $m, $y) = explode('/', $dob1);
$mk=mktime(0, 0, 0, $m, $d, $y);

0

#7 User is offline   jogasa21 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 09-March 11
  • Location:Costa Rica / Cambodia

Posted 15 June 2011 - 10:14 PM

View Postproto, on 25 November 2010 - 04:26 AM, said:

that may be done using purely CJuiDatePicker:

   		
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
                'model' => $model,
                'attribute' => 'dob',
                'options' => array(
                    'showAnim' => 'fold',
                    'dateFormat' => 'mm-dd-yy', // save to db format
                    'altField' => '#self_pointing_id',
                    'altFormat' => 'dd-mm-yy', // show to user format
                ),
                'htmlOptions' => array(
                    'style' => 'height:20px;'
                ),
            ));

However you still may want to Validate input server side.



Thanks proto for sharing this tip about the date format. It was really useful to me.
I also found out that in my project it was necessary to specify the name as follows:

<div class="row">
  <?php echo $form->labelEx($model,'Date Of Birth'); ?>
  <?php 
   $form->widget('zii.widgets.jui.CJuiDatePicker', array(
  	'model'=>$model,
  	'attribute'=>'dateofbirth',
  	'name'=>$model->dateofbirth,	// This is how it works for me.
  	'value'=>$model->dateofbirth,
  	'options'=>array('dateFormat'=>'yy-mm-dd', 
               		'altFormat'=>'dd-mm-yy', 
               		'changeMonth'=>'true', 
               		'changeYear'=>'true', 
               		'yearRange'=>'1920:2010', 
               		'showOn'=>'both',
               		'buttonText'=>'...'),
  	'htmlOptions'=>array('size'=>'10')
   ));
  ?>
  <?php echo $form->error($model,'dateofbirth'); ?>
</div>


If I just write
'name'=>'dateofbirth'
in that line of code then the value is not saved in my database.
0

#8 User is offline   jowen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 191
  • Joined: 13-July 11
  • Location:Malaysia

Posted 17 July 2011 - 08:53 PM

thank jogasa21. your answer solve my prob. XD
0

#9 User is offline   wyginwys 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 15-February 12

Posted 15 February 2012 - 05:07 AM

View Postproto, on 25 November 2010 - 04:26 AM, said:

that may be done using purely CJuiDatePicker:

$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model' => $model,
'attribute' => 'dob',
'options' => array(
'showAnim' => 'fold',
'dateFormat' => 'mm-dd-yy', // save to db format
'altField' => '#self_pointing_id',
'altFormat' => 'dd-mm-yy', // show to user format
),
'htmlOptions' => array(
'style' => 'height:20px;'
),
));

However you still may want to Validate input server side.

Hi proto
I tried your way. But without using beforeSave method mentioned above I couldnt update the changes in DatePicker box.
Could you explain how you have done this without beforeSave method?
0

#10 User is offline   msteel 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 25-February 12

Posted 25 February 2012 - 12:44 PM

Hi,

In my case, for I use the format "dd-mm-yy" in the frontend, but mySQL stores the value as "yy-mm-dd". Well, with your help, this code works for me.

In the view file:

	<?php $this->widget('zii.widgets.jui.CJuiDatePicker', 
	array(
	'model'=>$model,
	'attribute'=>'pc_fecha_nacimiento',
	'value'=>$model->pc_fecha_nacimiento,
	'language'=>'es',
	'options'=>array(
	    	'showAnim'=>'fold',
		'mode'=>'datetime',
		'dateFormat' => 'dd-mm-yy', // save to db format
		'changeMonth'=>'true', 
		'changeYear'=>'true', 
		'yearRange'=>'1900:2010', 
		'showOn'=>'both',
		'buttonText'=>'...',		
	),
    	'htmlOptions'=>array('style'=>'height:20px;'),
        )
        ); ?>


In the model file:

    protected function afterFind ()
    {
    	list($y, $m, $d) = explode('-', $this->pc_fecha_nacimiento);
	$mk=mktime(0, 0, 0, $m, $d, $y);
	$this->pc_fecha_nacimiento = date ('d-m-Y', $mk);
    	
        return parent::afterFind ();
    }

    protected function beforeSave ()
    {
    	list($d, $m, $y) = explode('-', $this->pc_fecha_nacimiento);
	$mk=mktime(0, 0, 0, $m, $d, $y);
	$this->pc_fecha_nacimiento = date ('Y-m-d', $mk);
		
        return parent::beforeSave ();
    }	

2

#11 User is offline   jmariani 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 12-January 12

Posted 29 November 2012 - 09:30 AM

Handling dates in the model is correct if you don't have a multiformat site (That is, you want to accept dates from different countries - and formats)
CJuiDatePicker must create a "hidden" field when altField option is set, so JuiDatePicker can populate the other field.
0

#12 User is offline   jmariani 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 12-January 12

Posted 29 November 2012 - 10:08 AM

Here's an approach. It involves changing core Yii CJuiDatePicker:

...
		if ($this->flat===false)
		{
			if($this->hasModel())
                            echo CHtml::activeTextField($this->model,$this->attribute,$this->htmlOptions);
			else
                            echo CHtml::textField($name,$this->value, $this->htmlOptions);

                        if (isset($this->options['altField'])) {
                            $hHtmlOptions = array();
                            chtml::resolveNameID($this->model, $this->options['altField'], $hHtmlOptions);
                            echo CHtml::activeHiddenField($this->model,$this->options['altField'], $hHtmlOptions);
                            $attribute = $this->options['altField'];
                            $this->options['defaultDate'] = $this->model->$attribute;
                            $this->options['altField'] = '#' . $hHtmlOptions['id'];
                        }
		}
		else

0

#13 User is offline   tomsea 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 66
  • Joined: 25-November 11

Posted 29 November 2012 - 10:59 AM

Thank you jmariani for sharing your approach.
0

#14 User is offline   MarkBrass 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 130
  • Joined: 13-November 13

Posted 13 April 2014 - 04:40 AM

Tried all the options here , fore some strange reason none worked apart from msteel's . . .
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