Yii Framework Forum: Different Format In Db And In Html Input Field - Yii Framework Forum

Jump to content

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

Different Format In Db And In Html Input Field how to convert between different formats for a CModel? Rate Topic: -----

#1 User is offline   Victor Porton 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 21-January 13

Posted 05 May 2013 - 10:06 AM

I have a MySQL database with date field in format like "2013-04-05".

I also have CActiveForm with datapicker which accepts values like "05/10/2013".

What is the proper way to convert between these formats when loading and when submitting the form?
0

#2 User is offline   Shahcheraghean 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 12-December 12
  • Location:Mashhad, IRAN

Posted 05 May 2013 - 11:15 AM

how dose the datepicker return date and time? is there any parameter that determine date format with it?
www.shgn.ir
www.shgn.ir/yii-framework/
yii2.swsco.ir
In a world without any fences or walls,Who needs Gates or Windows!
0

#3 User is offline   Victor Porton 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 21-January 13

Posted 05 May 2013 - 11:20 AM

View PostShahcheraghean, on 05 May 2013 - 11:15 AM, said:

how dose the datepicker return date and time? is there any parameter that determine date format with it?


It does not matter that it is done with datepicker. The only important thing is that I need to convert between different formats (one format in the DB and another in HTML).

Which method do I need to override to do format conversion?
0

#4 User is offline   VINAY Kr. SHARMA 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 151
  • Joined: 13-September 11
  • Location:Gurgaon

Posted 05 May 2013 - 11:25 AM

View PostVictor Porton, on 05 May 2013 - 10:06 AM, said:

I have a MySQL database with date field in format like "2013-04-05".

I also have CActiveForm with datapicker which accepts values like "05/10/2013".

What is the proper way to convert between these formats when loading and when submitting the form?


I think you should use php date function:

$db_date = '2013-04-05';

$date_to_html_format = date('d/m/Y', strtotime($db_date));



AND

$date_to_html_format = '05/10/2013';

$db_date = date('Y-m-d', strtotime($db_date));

VINAY Kr. SHARMA
@vinaykrsharma
Facebook ID: vinaykrsharma
Blog

Co-operation comes from friendship, friendship comes from trust, and trust comes from kind-heartedness.
0

#5 User is offline   Victor Porton 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 21-January 13

Posted 05 May 2013 - 11:27 AM

View PostVINAY Kr. SHARMA, on 05 May 2013 - 11:25 AM, said:

I think you should use php date function:

$db_date = '2013-04-05';

$date_to_html_format = date('d/m/Y', strtotime($db_date));



AND

$date_to_html_format = '05/10/2013';

$db_date = date('Y-m-d', strtotime($db_date));



The question is not how to do this, but WHERE (in which method) to do this?
0

#6 User is offline   dhanakumar 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 26-December 12
  • Location:Coimbatore,Tamilnadu

Posted 05 May 2013 - 11:59 AM

hi,

can you try this...

$tmpuser_expdate = date('Y-m-d', CDateTimeParser::parse($_POST['User']['umt_tmpuser_expdate'], 'm-d-y'));
-
Dhana.M
0

#7 User is offline   VINAY Kr. SHARMA 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 151
  • Joined: 13-September 11
  • Location:Gurgaon

Posted 05 May 2013 - 12:42 PM

View PostVictor Porton, on 05 May 2013 - 11:27 AM, said:

The question is not how to do this, but WHERE (in which method) to do this?


You can do that in view file and controller class and also in model class:

In View file:
...
$date_to_html_format = '';
if($model->some_date_field!==NULL)
$date_to_html_format = date('d/m/Y', strtotime($model->some_date_field));
echo '<input type="text" name="date_input" value="$date_to_html_format">';
...


Then Catch POST data in controller class:
...
$model = new SomeModelClass('some_scenario_here_if_any');
if(isset($_POST['SomeModelClass']) and count($_POST['SomeModelClass'])>0) {
	$model->attributes = $_POST['SomeModelClass'];
	// First you can change your date format here after checking submitted date is a real date or add validation rule to model class to validate value
	$model->some_date_field = date('Y-m-d', strtotime($model->some_date_field));
	if($model->save()) {
    	// DONE
	}
}
...


Alternatively you can parse date to DB format in model class function afterValidate() by overriding....
class SomeModelClass extends CActiveRecord() {
	...
	public function afterValidate() {
    	$this->some_date_field = date('Y-m-d', strtotime($this->some_date_field));
    	return parent::afterValidate();
	}
	...
}


Note if you use `afterValidate()` then date parsing in controller class is not required:
Remove below line if using `afterValidate()`
...
$model->some_date_field = date('Y-m-d', strtotime($model->some_date_field));
...

VINAY Kr. SHARMA
@vinaykrsharma
Facebook ID: vinaykrsharma
Blog

Co-operation comes from friendship, friendship comes from trust, and trust comes from kind-heartedness.
0

#8 User is offline   Fabrizio Caldarelli 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 285
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 06 May 2013 - 03:14 AM

View PostVictor Porton, on 05 May 2013 - 11:27 AM, said:

The question is not how to do this, but WHERE (in which method) to do this?


I'll make this in the controller
0

#9 User is offline   Shahcheraghean 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 12-December 12
  • Location:Mashhad, IRAN

Posted 07 May 2013 - 01:16 AM

In dependent on you, if you decide to use them in controller, voew or model, even if in an extension or any extra component class.
www.shgn.ir
www.shgn.ir/yii-framework/
yii2.swsco.ir
In a world without any fences or walls,Who needs Gates or Windows!
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