Yii Framework Forum: Problem Searching By Date Different Format - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem Searching By Date Different Format Problem searching by date different format Rate Topic: -----

#1 User is offline   ferminako 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 27-February 13

Posted 06 March 2013 - 03:41 AM

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

#2 User is offline   sebas 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 496
  • Joined: 28-October 08
  • Location:Buenos Aires, Argentina

Posted 06 March 2013 - 07:32 AM

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,


0

#3 User is offline   kokomo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 289
  • Joined: 23-July 10

Posted 06 March 2013 - 07:51 AM

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.
0

#4 User is offline   ferminako 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 27-February 13

Posted 06 March 2013 - 09:14 AM

View Postkokomo, on 06 March 2013 - 07:51 AM, said:

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 again..


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.
0

#5 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,315
  • Joined: 21-February 11
  • Location:Greece

Posted 06 March 2013 - 08:10 PM

View Postferminako, on 06 March 2013 - 09:14 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.


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

Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#6 User is offline   ferminako 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 27-February 13

Posted 07 March 2013 - 03:13 AM

View PostKonApaz, on 06 March 2013 - 08:10 PM, said:

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



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?
0

#7 User is offline   ferminako 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 27-February 13

Posted 07 March 2013 - 05:12 PM

no one?
0

#8 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,315
  • Joined: 21-February 11
  • Location:Greece

Posted 07 March 2013 - 06:05 PM

View Postferminako, on 07 March 2013 - 03:13 AM, said:

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?



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
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#9 User is offline   ferminako 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 27-February 13

Posted 09 March 2013 - 06:35 AM

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
0

#10 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,315
  • Joined: 21-February 11
  • Location:Greece

Posted 09 March 2013 - 10:50 AM

View Postferminako, on 09 March 2013 - 06:35 AM, said:

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


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 :)
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#11 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,315
  • Joined: 21-February 11
  • Location:Greece

Posted 09 March 2013 - 11:07 AM

View PostKonApaz, on 09 March 2013 - 10:50 AM, said:

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



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 :)
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#12 User is offline   ferminako 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 27-February 13

Posted 09 March 2013 - 12:34 PM

View PostKonApaz, on 09 March 2013 - 11:07 AM, said:

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


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
0

#13 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,315
  • Joined: 21-February 11
  • Location:Greece

Posted 09 March 2013 - 01:17 PM

View Postferminako, on 09 March 2013 - 12:34 PM, said:

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


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 :)
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#14 User is offline   Boris_YII 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 18-February 13

Posted 09 March 2013 - 02:19 PM

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

0

#15 User is offline   ferminako 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 27-February 13

Posted 10 March 2013 - 06:18 AM

KonApaz thanks for your regular expresion, it works :D
Boris_YII thanks you too!!

the problem was solved.
0

#16 User is offline   shness 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 27-November 12

Posted 03 August 2013 - 02:45 AM

View PostKonApaz, on 09 March 2013 - 01:17 PM, said:

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



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.





0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users