Cgridview Cutom Timezone

Hi everyone!

I’m working on a web application that would allow me to change the timezone of the dates in a CGridView. I have tested this in a custom controller action, the output makes sens and looks like: “2014-04-04 11:20” and “2014-04-04 18:20”. This is the code I used in this test controller:


public function actionIndex(){

	Yii::app()->setTimeZone("America/Montreal");

	echo date("Y-m-d H:i",1396624849);

		

	echo "<br>";

		

	Yii::app()->setTimeZone("Asia/Gaza");

	echo date("Y-m-d H:i", 1396624849);

}

Now I am trying to apply the same functionnality to a CGridView and it doesn’t work. Here is 2 codes I tried but didn’t work. Both Asia/Gaza and America/Montreal display the same date and I don’t know why. The ‘timeZone’ parameter was added to the main.php file. Here is the 2 attempts I made:


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

	'id'=>'reports-grid',

	'dataProvider'=>$dataProvider,

	'filter'=>$filtersForm,

	'columns'=>array(

		array(

			'header'=>'Date',

			'name'=>'inserted',

			'type'=>'raw',

			'value'=>function($data){

				Yii::app()->setTimeZone("Asia/Gaza");

				return date(Yii::app()->params->dateformat, strtotime($data->inserted));

			}

		))

));




Yii::app()->setTimeZone("Asia/Gaza");

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

	'id'=>'reports-grid',

	'dataProvider'=>$dataProvider,

	'filter'=>$filtersForm,

	'columns'=>array(

		array(

			'header'=>'Date',

			'name'=>'inserted',

			'type'=>'raw',

			'value'=>'value'=>'date(Yii::app()->params->dateformat, strtotime($data->inserted));'

		))

));

Thank you very much for your help.

Phil

Assuming you want them all to be the same time zone, have you tried something like this?




$dateTime = new DateTime();

$dateTime->setTimezone(new DateTimeZone('Asia/Gaza'));


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

        'id'=>'reports-grid',

        'dataProvider'=>$dataProvider,

        'filter'=>$filtersForm,

        'columns'=>array(

                array(

                        'header'=>'Date',

                        'name'=>'inserted',

                        'type'=>'raw',

                        'value'=>function($data) use ($dateTime){

                                $dateTime->setTimestamp(strtotime($data->inserted));

                                return $dateTime->format(Yii::app()->params->dateformat);

                        }

                ))

));



Completely untested, but I’d much rather use a local solution rather than changing settings for the whole request.

Amazing! It was what I was looking for. Thank you so much, it works like a charm.