Problem Searching By Date Different Format Problem searching by date different format
#1
Posted 06 March 2013 - 03:41 AM
//instead of this:
$criteria->compare('date1',$this->date1,true);
//I used this.
$criteria->compare('date("d-m-Y",strtotime(date1))',$this->date1,true);
But this doen't work, can anyone help me??
Thanks!
#2
Posted 06 March 2013 - 07:32 AM
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.
Regards,
#3
Posted 06 March 2013 - 07:51 AM
#4
Posted 06 March 2013 - 09:14 AM
kokomo, on 06 March 2013 - 07:51 AM, said:
I modiefied my model using:
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...
Thanks.
#5
Posted 06 March 2013 - 08:10 PM
ferminako, on 06 March 2013 - 09:14 AM, said:
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...
Thanks.
Hi my friend,
If the gridview displays the date as you want then I think you have to do that without override beforeFind or afterFind methods.
Instantly check this
$criteria->compare('date1',date("d-m-Y",strtotime($this->date1),true);
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
#6
Posted 07 March 2013 - 03:13 AM
KonApaz, on 06 March 2013 - 08:10 PM, said:
If the gridview displays the date as you want then I think you have to do that without override beforeFind or afterFind methods.
Instantly check this
$criteria->compare('date1',date("d-m-Y",strtotime($this->date1),true);
I need to use:
protected function afterFind () { $this->inicio=date("d-m-Y",strtotime($this->inicio)); parent::afterFind (); }
To show the date formated as d-m-Y because in de database the format is Y-m-d , then I tried your method:
$criteria->compare('date1',date("d-m-Y",strtotime($this->date1),true);
And nothing is displayed in the gridview.
Any idea?
#8
Posted 07 March 2013 - 06:05 PM
ferminako, on 07 March 2013 - 03:13 AM, said:
protected function afterFind () { $this->inicio=date("d-m-Y",strtotime($this->inicio)); parent::afterFind (); }
To show the date formated as d-m-Y because in de database the format is Y-m-d , then I tried your method:
$criteria->compare('date1',date("d-m-Y",strtotime($this->date1),true);
And nothing is displayed in the gridview.
Any idea?
hi ferminako
This code works for me:
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
Best regards
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
#9
Posted 09 March 2013 - 06:35 AM
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?
Thanks
#10
Posted 09 March 2013 - 10:50 AM
ferminako, on 09 March 2013 - 06:35 AM, said:
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?
Thanks
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
Stay tuned for the solution

It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
#11
Posted 09 March 2013 - 11:07 AM
KonApaz, on 09 March 2013 - 10:50 AM, said:
Stay tuned for the solution

Ok, change the code of search method
public function search() { $criteria = new CDbCriteria; $criteria->compare('id', $this->id); $criteria->compare('text', $this->text, true); //... another compares //the modified code $rev=$this->mdate; $rev = preg_replace('/^(\d{1,2})-(\d{1,2})$/',"$2-$1",$rev); //for day-month $rev = preg_replace('/^(\d{1,2})-(\d{1,2})-(\d{2,4})$/',"$2-$1-$3",$rev); //for day-month-year $criteria->compare('mdate', $rev,true); //end modified code return new CActiveDataProvider($this, array( 'criteria' => $criteria, )); }
Now everything will be ok

It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
#12
Posted 09 March 2013 - 12:34 PM
KonApaz, on 09 March 2013 - 11:07 AM, said:
public function search() { $criteria = new CDbCriteria; $criteria->compare('id', $this->id); $criteria->compare('text', $this->text, true); //... another compares //the modified code $rev=$this->mdate; $rev = preg_replace('/^(\d{1,2})-(\d{1,2})$/',"$2-$1",$rev); //for day-month $rev = preg_replace('/^(\d{1,2})-(\d{1,2})-(\d{2,4})$/',"$2-$1-$3",$rev); //for day-month-year $criteria->compare('mdate', $rev,true); //end modified code return new CActiveDataProvider($this, array( 'criteria' => $criteria, )); }
Now everything will be ok

First of all, thanks for your help, I'm really grateful, but using this modified code:
$criteria=new CDbCriteria; $criteria->compare('id',$this->id); //the modified code $rev=$this->inicio; $rev = preg_replace('/^(\d{1,2})-(\d{1,2})$/',"$2-$1",$rev); //for day-month $rev = preg_replace('/^(\d{1,2})-(\d{1,2})-(\d{2,4})$/',"$2-$1-$3",$rev); //for day-month-year $criteria->compare('inicio', $rev,true); //end modified code return new CActiveDataProvider($this, array( 'criteria'=>$criteria, ));
I can search by day-month and it works properly in all the cases(fe:01-02), but if I add the year(01-02-2013) nothing is displayed.
Maybe the regExpression doesn't fit with ...-YYYY
Thanks again, we are really close to solve it

#13
Posted 09 March 2013 - 01:17 PM
ferminako, on 09 March 2013 - 12:34 PM, said:
$criteria=new CDbCriteria; $criteria->compare('id',$this->id); //the modified code $rev=$this->inicio; $rev = preg_replace('/^(\d{1,2})-(\d{1,2})$/',"$2-$1",$rev); //for day-month $rev = preg_replace('/^(\d{1,2})-(\d{1,2})-(\d{2,4})$/',"$2-$1-$3",$rev); //for day-month-year $criteria->compare('inicio', $rev,true); //end modified code return new CActiveDataProvider($this, array( 'criteria'=>$criteria, ));
I can search by day-month and it works properly in all the cases(fe:01-02), but if I add the year(01-02-2013) nothing is displayed.
Maybe the regExpression doesn't fit with ...-YYYY
Thanks again, we are really close to solve it

Sorry, my fault!
replace the
by this one
$rev = preg_replace('/^(\d{1,2})-(\d{1,2})-(\d{2,4})$/',"$3-$2-$1",$r); //for day-month-year
Also this search works for
- day
- month
- year
- day-month
- day-month-year
will not work by
- month-year
if you want to do something more robust please inform me

It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
#14
Posted 09 March 2013 - 02:19 PM
$dateCriteria = new CDbCriteria(); $dateArray = explode('-', $this->date1); $day = $dateArray[0]; $month = $dateArray[1]; $year = $dateArray[2]; $date = $year . '-' . $month . '-' . $day; $dateCriteria->condition = 't.date1 LIKE "%' . $date . '%"'; $criteria->mergeWith($dateCriteria);
#15
Posted 10 March 2013 - 06:18 AM

Boris_YII thanks you too!!
the problem was solved.
#16
Posted 03 August 2013 - 02:45 AM
KonApaz, on 09 March 2013 - 01:17 PM, said:
replace the
by this one
$rev = preg_replace('/^(\d{1,2})-(\d{1,2})-(\d{2,4})$/',"$3-$2-$1",$r); //for day-month-year
Also this search works for
- day
- month
- year
- day-month
- day-month-year
will not work by
- month-year
if you want to do something more robust please inform me

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.