Problem with date comparison

Hi there,

After retrieval of a string variable dd-mm-yyyy, I converted it into a string format yyyy-mm-dd in order to perform an sql query from a mysql table where the attribute is a date attribute format yyyy-mm-dd. I need to compare my string variable with the date in the table. I am using MyTable::model()->find(date(myvariable) >= table_attribute) but it does not work and I have found that a find() without condition returns values for the date in the format dd-mm-yyyy which means that I do not have to convert my variable in the yyyy-mm-dd format.

I have tried a multiple of ways but have not succeeded in doing this. strotime function of php does not work with find() because I need to use mysql functions in the query. Can anybody please help?

Thanks

how exactly are you using the php date() function in your example. What is the variable that you pass to the function?

Thanks for your prompt reply. The code is in my controller. It is supposed to update a dropdown list in my form view where I need to have only the data which satisfies the condition carried out in the controller. It works fine with the one condition which is not related to date. But when I add another condition relative to the date, it is not giving satisfatory results.

Here is my code:




	public function actionRoomupdate()


	{

  		$roomtype = $_GET['Reservation']['roomtype'];

   	        $arrival = $_GET['Reservation']['arrival'];

  		$departure = $_GET['Reservation']['departure'];


		list($d, $m, $y) = explode('-', $arrival);

		$mk=mktime(0, 0, 0, $m, $d, $y);

		$arrival = date ('Y-m-d', $mk);

	

		list($d, $m, $y) = explode('-', $departure);

		$mk=mktime(0, 0, 0, $m, $d, $y);

		$departure = date ('Y-m-d', $mk);


      $data=Room::model()->findAll('roomtypeid=:id',

      array(':id'=> $roomtype));

      $data=CHtml::listData($data,'id','id');

      foreach($data as $value=>$id)  {

      $rep =Reservation::model()->find("room=$value and arrival = date($arrival)");

        if(empty($rep)) {   

      echo CHtml::tag('option',                   

      array('value'=>$value),CHtml::encode($id),true);            

      } else {echo 'aa'.$arrival.'aa'; echo 'bb'.($rep->arrival).'bb';  }//for debugging purposes    }

	}			




In one of my trials, I commented the conversion of the variable $arrival when I noted that the query returns $rep->arrival in the format mm-dd-yyyy. I had thought it would have returned it in the format yyyy-mm-dd.

Thanks for helping

as i said in ur previous post simply

using


date('Y-m-d',strtotime($model->ur-attribute-date));

this u can convert it to any format … and compare simply using ’ =, <, > ’ …

use like this




if(strtotime($date->target_date)>=strtotime($list['Date']))

or 

if(strtotime($date->target_date)=strtotime($list['Date']))

or 

if(strtotime($date->target_date)<strtotime($list['Date']))



Rajith, I want to put the condition in the find() method. Strtotime does not work here. If I do it outside the function, then I’ll have to use findAll() instead of find(). I wanted to avoid that. If I find only 1 occurrence satisfying the condition, I do not add it to the dropdown list. With findAll(), I’ll have to check for this condition in each element of the array which is longer. Maybe I am a bit lazy…

so write as criteria




$criteria->condition=$criteria->condition.' and '.'date_of_birth > :date_of_birth';

					  $criteria->params[':date_of_birth'] = date('Y-m-d',strtotime($_REQUEST['Students']['date_of_birth']));



so u just need to check equal to only ?? right… so change one to other’s format… using strtotime…assign to variable ,and then use in find()…

No, I need to check for greater, equal to and less than also. BTW, I had tried what you suggested with equal to but it does not work…

Hi,

You Just Need to write Between Condition in Bellowed Condition

$criteria->condition=$criteria->condition.’ and ‘.’<field Name> BETWEEN :dbarrival and :dbdepature’ ;

$criteria->params[’:dbarrival’] = date(‘Y-m-d’,strtotime($_REQUEST[’<Field Name>’]));

$criteria->params[’:dbdepature’] = date(‘Y-m-d’,strtotime($_REQUEST[’<Field Name>’]));

Thank you all,

I was making a stupid error: my variable was not inside quotes, it had to be like this:-




     $rep =Reservation::model()->find("room=$value && id<> $resid && arrival>'$arrival'");



@Jimlam so problem solved. right ? :)

Yes Rajith, problem solved :)

Many thanks, you were very helpful and learnt some interesting suggestions from you.

Thank u thank u :) :)

you can comparison your data manually like

$a = ‘how are you jobs’;

if (stripos($a,‘ARe’) && stripos($a,‘jobS’))

{

echo ‘yes it have’; } if(preg_match("/php/i", “PHP”) )

echo ‘pregmatch’;