Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Components/Filters/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
class Date extends Text
{
/** @var string */
protected $condition = 'BETWEEN ? AND ?';

/** @var string */
protected $formatValue;

Expand Down Expand Up @@ -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();
}

Expand Down
10 changes: 8 additions & 2 deletions tests/Components/Filter.Date.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down