[Solved] ask calculate difference datetime ?

sorry for late response

the content for degon : 2012-04-27 14:20:00

for degoff 2012-04-27 15:20:00

Since I can’t see where the problem may be, can you make an export of your table in sql format, and post it here, please?

Also can you post your full model, controller, and view, maybe attach them as files in order not to bloat this thread :slight_smile:

I’d like to see if I can debug that on my environment.

Sorry for late response

I have work in out of the city without internet connection

i attach the model , controller dan view

2817

pemadaman.zip

Thanks. But… come on ;) Your degon and degoff are varchar in the sql you attached. You have to change them to datetime.

Yes i Already change it to datetime …

I try to open view/pemadaman/admin but Error "Trying to get property of non-object"

Really? Your SQL says

[sql]CREATE TABLE IF NOT EXISTS pemadaman (

id int(11) NOT NULL AUTO_INCREMENT,

user_id int(11) NOT NULL,

sites_id int(11) NOT NULL,

degon varchar(20) NOT NULL,

degoff varchar(20) NOT NULL,

keterangan varchar(200) NOT NULL,

genoperasi int(10) NOT NULL,

PRIMARY KEY (id),

KEY user_id (user_id),

KEY sites_id (sites_id)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;[/sql]

Yes i change it last night when you find it :D

sorry for that mistake

But after i change it to datetime, the admin view is still error

And do you mind re-exporting the table in SQL format?

it’s already work know … i create the database, and the model from beginning… FUnction timediff already work

but why just the result of this column appear, another doesn’t appear

the result of admin view just like the picture

2829

appear.JPG

congratulations!

For your problem, I think you should… post your view code :)


<? function toTimeDifference($seconds)

{

    $d = (int)($seconds / 3600 / 24);

    $h = (int)(($seconds - $d*24*3600) / 3600);

    $m = (int)(($seconds - $d*24*3600 - $h*3600) / 60);

    $s = (int)($seconds - $d*24*3600 - $h*3600 - $m*60);

    return ($d ? $d . ' day' . ($d > 1 ? 's' : '') . ' ' : '') . ($h ? $h . ' hour' . ($h > 1 ? 's' : '') . ' ' : '') . ($m ? $m . ' minute' . ($m > 1 ? 's' : '') . ' ' : '') . ($s ? $s . ' second' . ($s > 1 ? 's' : '') . ' ' : '');

};?>


<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'pemadaman-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'id',

		'user_id',

		'degon',

		'degoff',

		array(

                'header'=>'Lama DEG Operasi',

                'value'=>'toTimeDifference($data->timeDifference)'

                ),

		/*

		'keterangan',

		'genoperasi',

		*/

	),

)); ?>

Model




public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;

		

		$criteria->compare('id',$this->id);

		$criteria->compare('user_id',$this->user_id);

		$criteria->compare('sites_id',$this->sites_id);

		$criteria->compare('degon',$this->degon,true);

		$criteria->compare('degoff',$this->degoff,true);

		$criteria->compare('keterangan',$this->keterangan,true);

		$criteria->compare('genoperasi',$this->genoperasi);

		$criteria->select = 'TIME_TO_SEC(TIMEDIFF(degoff, degon)) AS timeDifference';

		

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}



You should change this line into


$criteria->select = 'TIME_TO_SEC(TIMEDIFF(degoff, degon)) AS timeDifference, t.*';

or a more specific query of course

it’s clear now…

Thank you for your full support…

1 Last Question : How to convert datetime cjuidatepicker from datetime format like 2012-04-27 14:20:00 become timestamp in database ? or How to convert datetime in database 2012-04-27 14:20:00 become 27 April 2012 14:20:00 in views mode ? :D

Welcome and glad I could help.

Now for your question, I think we’ve already covered that (edited to reflect softark’s correction) for dates formatted like 27/04/2012 14:20 (see below). For another format, I guess you could do a little research by yourself about php’s date function formats.

I Already Found The Answer

For change Datetime to Timestamp in Database

I modified the function that you give me :D

In My Models


protected function beforeSave()

{

    list($day, $month, $year, $hour, $minute, $second) = split('[/ :]', $this->start_date);

    $this->tanggal = mktime($hour, $minute,$second, $month, $day, $year);

    return parent::beforeSave();

}



in My GridView




array(

     'name'=>'start_date', 

     'value'=>'CHtml::encode(date(\'j F, Y H:i\', $data->start_date))'

),

Thanks for your help…

That’s great! And you left timeDifference as is?

Edit: Notice that in http://php.net/manual/en/function.split.php it’s said

I just use this as my experiment… I still use timediff on my other models :D

Cause i don’t know how to timediff between timestamp, so the first i just learning how to convert to timestamp :D

Ok i already change it to




protected function beforeSave()

{

        list($c_date, $c_time) = explode(' ', $this->tanggal);

        list($c_d, $c_m, $c_y) = explode('/', $c_date);

        list($c_h, $c_i) = explode(':', $c_time);

        $this->tanggal = mktime($c_h, $c_i, 0, $c_m, $c_d, $c_y);

    return parent::beforeSave();

}



Hallo Mr Bennouna

I have another question but stil related with this datetime

  1. how to filter data just in one month and with certain parameter, for example i want to search how much the electric Off in April 2012 for One Location

  2. How to calculate the data of timedifference in a column like total accumulation ?

2880

2.JPG

Hello Mr Dedy,

Your first question is not clear to me.

For the second, it’s easy to find in the forum how to have totals. For instance:

[list=1]

[*]In your Model:


public static function getTotal($dp) {

    $total=0;

    foreach($dp->data as $item)

        $total += $item->amount;

    return $total;

}

[*]In your View:


$this->widget('zii.widgets.grid.CGridView', array(

    'id'=>'some-grid',

    'dataProvider'=>$model->search(),

    'columns'=>array(

        …

        array(

            'value'=>'$data->amount',

            'header'=>'Amount',

            'footer'=>$model->search()->itemCount == 0 ? '0' : $model->getTotal($model->search()),

        ),

        …

    )

));

[/list]

For the First Question, I Mean, How to filter the data based on the date. Example I Just want to see data on One Month. I Want to filter the data like dropdown list which we can choose each month based on data : example

2012-04-01 to 2012-04-30( in drop down list will appear like April 2012)