More information about this topic
Reset database
 | 
Select your language: English  Italian 
User input advanced example

Click a row to edit..

Note: the Yii::app()->lc component is a custom component (LocaleManager); click here for more information.

Displaying 1-2 of 2 results.
DateTimeDate and time
06/24/201008:15:0006/02/2010 09:15:00
06/22/201009:33:2106/21/2010 11:32:10

protected/modules/InternationalizationModule/views/datetime/userinput.php


$this->widget('zii.widgets.grid.CGridView', array(
	'dataProvider' => $model->search(),
	'selectionChanged'=>'updateEditForm',
	'id'=>'main_table',
	'columns' => array(
		array(
			'name' => 'c_date',
			'type' => 'raw',
			'htmlOptions' => array('class'=>'date_cell'),
			'value' => 'Yii::app()->lc->toLocal($data->c_date, "date", "small")',
		),
		array(
			'name' => 'c_time',
			'type' => 'raw',
			'htmlOptions' => array('class'=>'time_cell'),
			'value' => 'Yii::app()->lc->toLocal($data->c_time, "time", "small")',
		),
		array(
			'name' => 'c_datetime',
			'type' => 'raw',
			'htmlOptions' => array('class'=>'datetime_cell'),
			'value' => 'Yii::app()->lc->toLocal($data->c_datetime, "datetime", "small")',
		),
	),
));

?>

<div class="form">
<?php echo CHtml::beginForm(); ?>
<div class="row">
	<?php echo CHtml::activeLabelEx($model,'c_date'); ?>
	<?php $this->widget('zii.widgets.jui.CJuiDatePicker',
		array(
			'name'=>'DtTest[c_date]',
			'options' => array('dateFormat'=>Yii::app()->locale->getDateFormat('calendar_small')),
			'htmlOptions' => array('readonly'=>"readonly")
		)
	); ?>
</div>
<div class="row">
	<?php echo CHtml::activeLabelEx($model,'c_time'); ?>
	<?php echo CHtml::dropdownList('c_time_hour', false, $hour_arr); ?>
	<?php echo CHtml::dropdownList('c_time_min', false,
		array('00'=>'00', '15'=>'15', '30'=>'30', '45'=>'45')); ?>
</div>
<div class="row">
	<?php echo CHtml::activeLabelEx($model,'c_datetime'); ?>
	<?php $this->widget('zii.widgets.jui.CJuiDatePicker',
		array(
			'name'=>'DtTest[c_datetime]',
			'options' => array('dateFormat'=>Yii::app()->locale->getDateFormat('calendar_small')),
			'htmlOptions' => array('readonly'=>"readonly")
		)
	); ?>
	<?php echo CHtml::dropdownList('c_datetime_hour', false, $hour_arr); ?>
	<?php echo CHtml::dropdownList('c_datetime_min', false,
		array('00'=>'00', '15'=>'15', '30'=>'30', '45'=>'45')); ?>
</div>

<?php echo CHtml::hiddenField('item_id', 0); ?>
<?php echo CHtml::ajaxButton('Save', $this->createUrl('userinputSave'),
	array('type'=>'POST', 'success'=>'formSaved'), array('disabled'=>"disabled", 'id'=>'save_btn')); ?>

<?php echo CHtml::endForm(); ?>
</div>

protected/modules/InternationalizationModule/controllers/DatetimeController.php

	public function actionUserinputData() {
		$item_id =(int)$_GET['id'];

		$model =DtTest::model()->findByPk($item_id);

		if ($model != null) {
			$c_time =explode(':', $model->c_time);
			$dt_info =Yii::app()->lc->splitDatetime($model->c_datetime, false, 'database');
		}

		$res =array(
			'c_date'=>Yii::app()->lc->toLocal($model->c_date, 'date', 'small'),
			'c_time_hour'=>$c_time[0],
			'c_time_min'=>$c_time[1],
			'c_datetime'=>Yii::app()->lc->toLocal($dt_info['date'], 'date', 'small'),
			'c_datetime_hour'=>$dt_info['hour'],
			'c_datetime_min'=>$dt_info['min'],
		);

		echo CJSON::encode($res);
	}

protected/modules/InternationalizationModule/controllers/DatetimeController.php

	public function actionUserinputSave() {

		if (isset($_POST['item_id']) && $_POST['item_id'] > 0 && isset($_POST['DtTest'])) {
			$item_id =(int)$_POST['item_id'];

			$model =DtTest::model()->findByPk($item_id);
			$model->c_date =Yii::app()->dateFormatter->formatDateTime(
				CDateTimeParser::parse(
					$_POST['DtTest']['c_date'],
					Yii::app()->locale->getDateFormat('small')
				),
				'database',
				false
			);

			$model->c_time =$_POST['c_time_hour'].':'.$_POST['c_time_min'].':00';

			$model->c_datetime =Yii::app()->lc->mergeDatetime(
				array(
					'date'=>$_POST['DtTest']['c_datetime'],
					'hour'=>$_POST['c_datetime_hour'],
					'min'=>$_POST['c_datetime_min'],
				),
				'database', 'small'
			);

			$model->save();
		}
	}
Fork me on GitHub