Problem Searching By Date Different Format


(Fgcaipe) #1

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 :

//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!


(Sebathi) #2

Hi,

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,


(Mwerlberger85) #3

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.


(Fgcaipe) #4

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.


(Konapaz) #5

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(‘date(“d-m-Y”,strtotime(date1))’,$this->date1,true);


$criteria->compare('date1',date("d-m-Y",strtotime($this->date1),true);


(Fgcaipe) #6

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?


(Fgcaipe) #7

no one?


(Konapaz) #8

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


(Fgcaipe) #9

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?

Thanks


(Konapaz) #10

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 :)


(Konapaz) #11

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 :)


(Fgcaipe) #12

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 :D


(Konapaz) #13

Sorry, my fault!

replace the

$rev = preg_replace(’/^(\d{1,2})-(\d{1,2})-(\d{2,4})$/’,"$2-$1-$3",$rev); //for day-month-year

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 :)


(Boris Hrantovich) #14

Hope this will help




$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);




(Fgcaipe) #15

KonApaz thanks for your regular expresion, it works :D

Boris_YII thanks you too!!

the problem was solved.


(Shness) #16

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.