CGridview and Date formatting

Hey guys, I have a problem:

I’m trying to format the time I show in the list as AM/PM time. So, in my database I store time as DATETIME.

The values that are stored in the database are 2012-01-01 00:00:00. When the client is created the DATETIME is NULL.

But when I try to display it in CGridview I need to use strtotime. The problem is that strtotime defaults NULL value to a random time instead of not displaying anything.

My question is, is there a way around it? Maybe some kind of Yii function I’m missing?




<?php 

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

	'id'=>'customer_id',

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

	'filter'=>$model,

	'enablePagination'=>true,

	'updateSelector'=>".updatebutton",			

	'cssFile'=> Yii::app()->request->baseUrl . "/css/grid-view.css",

	'htmlOptions'=>array('data-enhance'=>'false',),

	'pager'=>array('cssFile'=>Yii::app()->request->baseUrl . '/css/pager.css', 'header' => "", 

	"prevPageLabel" => "Prev", "nextPageLabel" => "Next"),

			'columns'=>array(								          'party_name',

       'phone',									

	array(

	'name'=>'wait_time',

	'value'=>'date("h:i A", [u]strtotime($data->wait_time)[/u])',),

	array(

	'name'=>'final_time',

	'value'=>'date("h:i A", [u]strtotime($data->final_time)[/u])',),									

	'groupsize',

	array(

	'class'=>'CButtonColumn',

	'template'=>'{update}',

        'updateButtonUrl'=>'Yii::app()->controller->createUrl("/client/waiting-update",array("id"=>$data->customer_id))',

	'updateButtonImageUrl'=>Yii::app()->request->baseUrl . '/images/update.png',

	'updateButtonOptions'=>array("rel"=>"external"),

	),      

	),

));


?>

you could write a function to convert the str according to your needs. just for example:


'value' => 'MyHelperClass::myDateFormatter($data->final_time)',

and the function can display a null or "zero" date as you choose.

Or you could just ternary it in the value part:




array(

     ...

     'value'=>'$data->final_time ? date("h:i A", strtotime($data->final_time)) : 0'

     ...

),



can’t get you properly.

if you want to show value only if its not empty then go with third post,

and if you want to do some modification in value data then create function in model and go with second post.

Thanks for replies!!!

I went with 2nd post.

Here is my function:





public function convertTime($model,$str)

	{		

	

		if($str != null)

		{

			$str = date("h:i A", strtotime($model->wait_time));

		}

		else

		{

			$str = '';

		}

		

		return $str;

	}

	




Here is the view:





array(

						   'name'=>'wait_time',												

'value'=>'****Controller::convertTime($data,$data->wait_time)'),

					

array(

'name'=>'final_time',						'value'=>'****Controller::convertTime($data,$data->final_time)'),




Unfortunatly the view final_time displays the same time as wait_time. Dont know why because in database those times are different. So I’m now looking through the code to find an error.

EDIT: found the error…sorry!!!




public function convertTime($model,$str)

	{		

	

		if($str != null)

		{

			$str = date("h:i A", strtotime($str));

		}

		else

		{

			$str = '';

		}

		

		return $str;

	}

	




You’re returning $model->wait_time in the function. Change the function to not include the model part:




public function convertTime($str)

        {               

        

                if($str != null)

                {

                        $str = date("h:i A", strtotime($str));

                }

                else

                {

                        $str = '';

                }

                

                return $str;

        }




This is pretty much the same as this in the value part, though:




 'value'=>'$data->final_time ? date("h:i A", strtotime($data->final_time)) : ""'



Or you could change the function to include the attribute:




public function convertTime($model, $attribute)

        {               

        

                if($model->attribute != null)

                {

                        $str = date("h:i A", strtotime($model->attribute));

                }

                else

                {

                        $str = '';

                }

                

                return $str;

        }




There are multiple formatting types this column class can use. In order to switch between these you can set type property to the one of the following:

  1. raw — the attribute value will be rendered as is so it’s especially useful in case of complex formatting. Be careful with this one and make sure output is properly escaped.

  2. text (default) — the attribute value will be HTML-encoded.

  3. ntext — formats the value as a HTML-encoded plain text and converts newlines with HTML <br/> tags.

  4. html — the attribute value will be sanitized using HTMLPurifier. Note that it’s not that good for performance so don’t overuse it.

  5. date — formats the value as a data using PHP’s date and dateFormat property as format string. Default format is Y/m/d.

  6. time — formats the value as a time using PHP’s date and timeFormat property as format string. Default format is h:i:s A.

  7. datetime — formats the value as a date and time using PHP’s date and datetimeFormat property as format string. Default format is Y/m/d h:i:s A.

8) boolean — value is formatted as boolean. By default that means a simple Yes or No text. These could be overridden using booleanFormat property.

  1. number — formats the value as a number using PHP number_format() function.

  2. email — formats the value as a mailto link using CHtml::mailto.

  3. image — formats the value as an image tag using CHtml::image.

  4. url — formats the value as a hyperlink.