I am trying to display created date column in CGridView, in DB its datatype is DateTime.
I have used the below code to format the date,
array( 'name'=>'created',
'header'=>'created',
'value' => 'Yii::app()->dateFormatter->format("d/M/y",strtotime($data->created))'
),
I am getting date formated column in CGridView, but null values shows sysdate in column. Please see attached image,
Please provide any solution to remove sysdate in null values column
Well, strtotime(null) will return false (Kami is apparently wrong).
The issue is in Yii date formater : Yii::app()->dateFormatter->format("d/M/y", false) will return current date.
You should simply create a getter in your model :
function getCreatedDate()
{
if ($this->created===null)
return;
return Yii::app()->dateFormatter->format("d/M/y", $this->created);
}
And in your grid view columns, you just have to use :
array('name'=>'created', 'value'=>'$data->createdDate'),
or 'createdDate'.
Use nullDisplay property of CGridView and set:
'nullDisplay'=>''
Related
In my Symfony Application i have a From which contains a lot of checkboxes, radios, textfields and also DateTime Objects.
Everything but the DateTime Objects work fine, but with them i always get the error "Object of class DateTime could not be converted to string"
So i wanted to use this tutorial to change the datetime object to a string.
The thing is i am using a FormFactory and when i use the addModelTransformer()-Method like this...
1st Option:
$form->add($formFactory->createNamed('value', 'date', null, $fieldOptions));
$form->get('value')->addModelTransformer(new CallbackTransformer(
function($dateAsDate){
return $dateAsDate->format('Y-m-d');
},
function ($dateAsString){
return "test"; //TODO: to be changed to make string to date
}
));
...i get the Error "Call to undefined method Symfony\Component\Form\Form::addModelTransformer() "
When i use the builder function outside the Formfactory like this...
2nd Option:
//$builder->addEventListener Stuff is above
$builder->get('value')->addModelTransformer(new CallbackTransformer(
function($dateAsDate){ return $dateAsDate->format('Y-m-d'); },
function ($dateAsString){ return 'null'; } //TODO: to be changed to make string to date
));
...i get the Error "The child with the name "value" does not exist."
Does anybody have an idea how to make this work?
There is no need to use any custom Tranformer here.
The DateType field handles DateTime objects correctly by itself.
You may want to specify some options like 'format' and 'widget' depending on how you want to render your field, but the default 'input' should work fine with DateTime.
For example :
$builder->add('value', DateType::class, [
'format' => 'y-MM-dd',
'widget' => 'single_text', // To have a single input text box
'input' => 'datetime' // This is the default value
];
I have field last_active on users, I want display time with diffForHumans or time_from_now from Moment.js. How I can do it? Now I just use:
DateTime::make('Activiy', 'last_active')
->exceptOnForms(),
When I use:
DateTime::make('Activiy', 'last_active')->diffForHumans()
->exceptOnForms(),
I get undifined method diffForHumans.
To my knowledge at the moment DateTime only supports format.
Since you want only to display, you can try Text field & display the humanise value. If your column might be null be sure to check for that otherwise you will get an error.
Text::make('Activiy', 'last_active')
->displayUsing(function($lastActive) {
if ($lastActive === null) {
return null;
}
return $lastActive->diffForHumans();
})
->exceptOnForms(),
Plugin: FriendsOfCake/Search
CakePHP Version: 1.3.4
I use the search plugin to filter data with a form in a view. In the database, fields of type DATE have the format YYYY-mm-dd but in the input field on the page the data needs to be inserted like dd.mm.YYYY by the user.
So I need to convert the date inserted in the form field to the format used in the database before values are compared.
What I don't get is where to actually "grab" the value to do the convertion within the plugins setup. I tried some things in the controller to either convert "return_ticket" or "opening_date" before they are compared with each other but I can't figure out anything that's working. Can someone help me with this?
At which point could I do the date/string convertion in the following setup?
// plugin settings in table
class TicketsTable extends Table
{
public function searchConfiguration()
{
$search = new Manager($this);
$search->value('ticket_return',[
'field' => $this->Appointments->target()->aliasField('opening_date')
]);
return $search;
}
// search setup in controller
class TicketsController extends AppController
{
public function index()
{
$query = $this->Tickets
->find('search',
$this->Tickets->filterParams($this->request->query))
}
}
// search form in view
<?= $this->Form->input(
'ticket_return', [
'label' => 'search date'
'default' => $this_date // today
])
?>
In my form, I created the value by populating the dropbox from values from a table.
<?php echo $form->dropDownList($model,'status', CHtml::listData(Statusprospect::model()->findAll(), 'id', 'status'),array('prompt' => 'Select')); ?>
When I view the record it has a 1, as it should for status. How do I make it display the value when the record is viewed, instead of the 1.
The view file code that currently displays the field is this:
<?php echo CHtml::encode($data->status); ?>
The Model does have the relationship defined:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'status0' => array(self::BELONGS_TO, 'Statusprospect', 'status'),
);
}
How would I accomplish showing the value instead of the number?
Right now this should work $data->status0->status.
Take care that $data->status0->status might not be set if $data->status can be null so make a check beforehand if that is the case. You can use
CHtml::encode(isset($data->status0->status) ? $data->status0->status : '-');
I'd like to have a field inside a form, which has a format like dd.MM.yyyy HH:mm. I've done this with the date validator.
But now I have the problem, that the user always have to enter a time, not only the specified day. Is it possible automatically set the time to 00:00 when the user only enters the date?
Thanks for your help.
protected function beforeSave()
{
if ( strpos(trim(this->date_field), ' ') !== false ) {
$this->date_field = sprintf('%s 00:00', $this->date_field);
}
return parent::beforeSave();
}