Hi veryone , I show the dates on my gridviews like d-m-y and when I’am gonna use the search, it doesn’t work obviously because when yii compare one date is format like Y-m-d(model) and in the gridView is like d-m-Y so I tried to fix it changin on the model the compare instructiond search in function search like :
It will be better to check the format of the date in your PHP code to make (search scenario of your model if you are using it) and add a rule that ensures to change it if it’s needed.
You can overwrite beforeFind/afterFind or some other before/afterXXX methods to do the date conversion before search() is called and back afterwards for display in view.
protected function afterFind ()
{
$this->inicio=date("d-m-Y",strtotime($this->inicio));
parent::afterFind ();
}
So now, all my views render the date with this format , but if I try to use :
protected function beforeFind ()
{
$this->inicio=date("Y-m-d",strtotime($this->inicio));
parent::beforeFind ();
}
In order to convert the date before I try to search whatever from my grid finder , It doesn’t work…so I deleted that, what do I have to do?The search still works using Y-m-d format but I want to use d-m-Y , and I don’t know if for this search I need to do other thing, because this search uses ajax (because I don’t see any refresh, so I gues it…) and because of the view render d-m-Y I supposed that I don’t need to modify anything…
protected function afterFind ()
{
$this->mdate=date("d-m-Y",strtotime($this->date1));
parent::afterFind ();
}
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id);
$criteria->compare('text', $this->text, true);
//... anothe compares
$criteria->compare('mdate', $this->date1,true); //DO NOT modified that
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
//in addition in view file I didn't modify any part of
$this->widget('zii.widgets.grid.CGridView', array
....
....
So check your code with my issue, or make a new test model-view-controller to test my code issue
Hi KonApaz, It’s funny, it doesn’t work properly… for example I’ve got in on my gridview several rows with dates like 04-03-2013,04-03-2013,04-03-2013,04-03-2013,04-03-2013,13-03-2013…
If I search by 04-03 nothing is displayed, but if I search by 13-03 the row of the date 13-03-2013 is displayed but if I add to the search 13-03 the year like 13-03-2013 nothing is display… where can be the error?
This is happened because the afterFind modify the final results on view or logic, but in searching the compare method get the value of the user (fox example 04-03) and search reversing (03-04) on the database, So my code has a bug
Isn’t the semantic meaning of the date lost with that expression. I have the date 03-05-2013. Is it March 5th or May 3rd?
One solution (may not be the best) is to use the standard database format. For MySQL it is Y-m-d h:i:s. Then use that throughout the app. When you need to render a date just format the date object with a format you set in the params array of your config. You now control the date format globally. That works, but whether it is the best, I can’t say.
If you have users all over the world, they will expect different formats and this isn’t the best approach.