Date Difference

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,

  1. link

localhost/mywebsite?r=site/datepicker

  1. In the siteController add datepicker action

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);


}

}

  1. the corresponding view named datepicker

<?php

//@var $this SiteController

$this->pageTitle = ‘Difference between two dates’;

?>

<div class="form">

<?php

if(!$auto_disable) :

$form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'date-form',

)); ?>

<div class="row">

<?php echo $form->labelEx($model,‘date_start’); ?>

<?php

$this->widget(‘zii.widgets.jui.CJuiDatePicker’, array(

'model' =&gt; &#036;model,


'attribute' =&gt; 'date_start',


'options' =&gt; array(


 'dateFormat' =&gt; 'yy-mm-dd',


 ),


'htmlOptions' =&gt; array(


    'size' =&gt; '10',         // textField size


    'maxlength' =&gt; '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' =&gt; &#036;model,


'attribute' =&gt; 'date_end',


'options' =&gt; array(


 'dateFormat' =&gt; 'yy-mm-dd',


 ),


'htmlOptions' =&gt; array(


    'size' =&gt; '10',         // textField size


    'maxlength' =&gt; '10',    // textField maxlength


),

));

?>

<?php echo $form->error($model,‘date_start’); ?>

</div>

&lt;div class=&quot;row buttons&quot;&gt;


	&lt;?php echo CHtml::submitButton('Do the difference'); ?&gt;


&lt;/div&gt;

<?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>