diff --git a/src/Components/Filters/Date.php b/src/Components/Filters/Date.php index 397980ac..11296c1c 100644 --- a/src/Components/Filters/Date.php +++ b/src/Components/Filters/Date.php @@ -23,6 +23,9 @@ */ class Date extends Text { + /** @var string */ + protected $condition = 'BETWEEN ? AND ?'; + /** @var string */ protected $formatValue; @@ -94,9 +97,13 @@ public function __getCondition($value) { $condition = $this->condition; if ($this->where === NULL && is_string($condition)) { - $column = $this->getColumn(); - return ($date = \DateTime::createFromFormat($this->dateFormatInput, $value)) - ? Condition::setupFromArray(array($column, $condition, $date->format($this->dateFormatOutput))) + $date = \DateTime::createFromFormat($this->dateFormatInput, trim($value)); + $values = $date + ? array($date->format('Y-m-d') . ' 00:00:00', $date->format('Y-m-d') . ' 23:59:59') + : NULL; + + return $values + ? Condition::setup($this->getColumn(), $this->condition, $values) : Condition::setupEmpty(); } diff --git a/tests/Components/Filter.Date.phpt b/tests/Components/Filter.Date.phpt index 5dfd410b..cc62f851 100644 --- a/tests/Components/Filter.Date.phpt +++ b/tests/Components/Filter.Date.phpt @@ -30,14 +30,20 @@ class FilterDateTest extends \Tester\TestCase $grid = new Grid; $filter = $grid->addFilterDate('date', 'Date'); - Assert::same(array('date LIKE ?', '2012-12-21%'), $filter->__getCondition('21.12.2012')->__toArray()); + Assert::same( + array('date BETWEEN ? AND ?', '2012-12-21 00:00:00', '2012-12-21 23:59:59'), + $filter->__getCondition('21.12.2012')->__toArray() + ); Assert::same(array('0 = 1'), $filter->__getCondition('TEST BAD INPUT')->__toArray()); $filter ->setDateFormatInput('d/m/Y') ->setDateFormatOutput('d.m.Y'); - Assert::same(array('date LIKE ?', '21.12.2012'), $filter->__getCondition('21/12/2012')->__toArray()); + Assert::same( + array('date BETWEEN ? AND ?', '2012-12-21 00:00:00', '2012-12-21 23:59:59'), + $filter->__getCondition('21/12/2012')->__toArray() + ); Assert::same(array('0 = 1'), $filter->__getCondition('21.12.2012')->__toArray()); //test bad input } }