Hi all,
I want to find date difference between 2 date pickers and display the result in result text box.
can anyone provide me code for it.
ThankYou
Hi all,
I want to find date difference between 2 date pickers and display the result in result text box.
can anyone provide me code for it.
ThankYou
Hello,
localhost/mywebsite?r=site/datepicker
public function actiondatepicker()
{
$model = new datepicker;
if(isset($_POST['datepicker']))
{
$model->attributes = $_POST['datepicker'];
if($model->validate() && $model->isCorrect())
$this->render('datepicker', array('auto_disable' => true, 'date_diff_value' => $model->getdiff()));
else
$this->redirect(Yii::app()->request->getUrl());
}else {
//Display the view who use DatePicker of JQuery...
$this->render('datepicker', array('auto_disable' => false,'model' => $model));
}
}
3.I use datepicker model who inherit from CFormModel
class datepicker extends CFormModel
{
public $date_start;
public $date_end;
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('date_start,date_end', 'required'),
array('date_start', 'date', 'format' => 'yyyy-MM-dd'),
array('date_start', 'length', 'is' => 10),
array('date_end', 'date', 'format' => 'yyyy-MM-dd'),
array('date_end', 'length', 'is' => 10),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('', 'safe', 'on'=>'search'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'date_start' => 'Date start',
'date_end' => 'Date End',
'date_diff' => 'Difference between the two dates'
);
}
/*
* Compare the dates
* this method return true if date start is trully inferior to date_end
*/
public function isCorrect()
{
if($this->date_start !== null AND $this->date_end !== null)
{
//formate date_start to yyyymmdd
$date_start = new DateTime($this->date_start);
$date_start = $date_start->format('Ymd');
//formate date_end to yyyymmdd
$date_end = new DateTime($this->date_end);
$date_end = $date_end->format('Ymd');
if($date_start <= $date_end)
return true;
else
return false;
}
else
return false;
}
//this method get difference between the two dates...
public function getDiff()
{
$date_start = new DateTime($this->date_start);
$date_end = new DateTime($this->date_end);
return (strtotime($this->date_end) - strtotime($this->date_start)) / (60*60*24);
}
}
<?php
//@var $this SiteController
$this->pageTitle = ‘Difference between two dates’;
?>
<div class="form">
<?php
if(!$auto_disable) :
$form=$this->beginWidget(‘CActiveForm’, array(
'id'=>'date-form',
)); ?>
<div class="row">
<?php echo $form->labelEx($model,‘date_start’); ?>
<?php
$this->widget(‘zii.widgets.jui.CJuiDatePicker’, array(
'model' => $model,
'attribute' => 'date_start',
'options' => array(
'dateFormat' => 'yy-mm-dd',
),
'htmlOptions' => array(
'size' => '10', // textField size
'maxlength' => '10', // textField maxlength
),
));
?>
<?php echo $form->error($model,‘date_start’); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,‘date_end’); ?>
<?php
$this->widget(‘zii.widgets.jui.CJuiDatePicker’, array(
'model' => $model,
'attribute' => 'date_end',
'options' => array(
'dateFormat' => 'yy-mm-dd',
),
'htmlOptions' => array(
'size' => '10', // textField size
'maxlength' => '10', // textField maxlength
),
));
?>
<?php echo $form->error($model,‘date_start’); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Do the difference'); ?>
</div>
<?php
$this->endWidget();
else :
?>
<div id="row">
<?php echo CHtml::label(‘Difference’, ‘date_diff’);?>
<?php echo CHtml::textField(‘date_diff’, $date_diff_value.’ days’, array(‘disabled’ => ‘disabled’));?>
</div>
<?php endif;?>
</div>