We are using Yii Framework and have created a search form to include the following DatePicker widge:
<?php echo $form->labelEx($model, 'availability_start_date');?>
<?php
Yii::import('zii.widgets.jui.CJuiDatePicker');
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name'=>'stattDate',
'options'=>array(
'showAnim'=>'fold',
),
'htmlOptions'=>array(
'style'=>'height:20px;'
)
));
?>
This widget needs to be used to search for users who have an availability_start_date +/- 2 days from the value specified in the above widget.
Our UserController has the following join logic:
if ($search_frm['availability_start_date']){
$join .= ' LEFT JOIN user_availability_start_date usd on u.id = usd.id';
$where .= 'AND usd.availability_start_date >= '.$search_frm
['availability_start_date'];
}
Currently the logic in the WHERE clause just asks for a match where the availability_start_date is greater than or equal to the widget’s value.
How do I modify the above WHERE clause to select those records where the widget’s value is +/- 2 days different than the availability_start_date value?
UPDATE: I’ve revised the where clause to read as follows:
if ($search_frm['availability_start_date']){
$join .= ' LEFT JOIN user_availability_start_date usd on u.id = usd.id';
$where .= ' AND usd.availability_start_date >= DATE_ADD('.$search_frm.',
INTERVAL -2 DAY)
AND usd.availability_start_date <= DATE_ADD(' .$search_frm.', INTERVAL 2 DAY)';
}
Unfortunately, when I test the logic, records are returned that don’t meet this criteria. No errors are thrown on the form or page.
DATEDIFF()returns a value in days. For +/- 2 days you will want to use the absolute valueABS()returned byDATEDIFF(), verify that it is<= 2.I have also wrapped the arguments to
DATEDIFF()inDATE()to truncate off the time portions if they are present. Remove those if they are unnecessary for your application.