Compare Date (Converted To Datetime) With Datetime

I had a datetime field in DB table.

I want to add date range search in admin Gridview.

Using CJuiDatePicker i have succesfully got date rangle.

Then In Search() function in my Model i am using the code bellow:




if(!empty($this->date_to) && !empty($this->date_from))

        {

			$date1= new date($this->date_from);

			$date11=$date1->format('Y-m-d h:m:s');

			$date2= new date($this->date_to);

			$date22=$date2->format('Y-m-d h:m:s');

            $criteria->condition = "date >= $date11 AND date <= $date22";

        }



But it’s not working. Can’t find the issue. Other search criteria is working fine.

Please help me.

Thanks in advance…

When you put values directly in the sql query, which you shouldn’t do ever, you can to quote them if they’re not numbers:




$criteria->condition = "date >= '$date11' AND date <= '$date22'";



or better yet:




$criteria->condition = "date BETWEEN :date_from AND :date_to";

$criteria->params[':date_from'] = $date11;

$criteria->params[':date_to'] = $date22;



Even if those values don’t come from the users directly it’s a good habbit to always use value binding.

Thanks for your quick reply.