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
2 changes: 2 additions & 0 deletions lib/validator/sfValidatorDate.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ protected function doClean($value)
else
{
$dateMax = new DateTime($max);
$dateMax->setTimezone(new DateTimeZone(date_default_timezone_get()));
Copy link
Contributor

@alquerci alquerci Dec 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @nederdirk,

Here is not an issue with the time zone but the way where the date time are compared.

The proper way to do it is to compare timestamps.

As the timestamp does not depend on the timezone then all issue will be resolved there.

$max = $dateMax->format('YmdHis');
$maxError = $dateMax->format($this->getOption('date_format_range_error'));
}
Expand All @@ -140,6 +141,7 @@ protected function doClean($value)
else
{
$dateMin = new DateTime($min);
$dateMin->setTimezone(new DateTimeZone(date_default_timezone_get()));
$min = $dateMin->format('YmdHis');
$minError = $dateMin->format($this->getOption('date_format_range_error'));
}
Expand Down
34 changes: 33 additions & 1 deletion test/unit/validator/sfValidatorDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

require_once(__DIR__.'/../../bootstrap/unit.php');

$t = new lime_test(52);
$t = new lime_test(56);

$v = new sfValidatorDate();

Expand Down Expand Up @@ -244,3 +244,35 @@
// did it convert from the other timezone to the default timezone?
$date->setTimezone($defaultTimezone);
$t->is($clean, $date->format('Y-m-d H:i:s'), '->clean() respects incoming and default timezones');

// 'min' and 'max' options should be normalized to the default timezone
$v = new sfValidatorDateTime(array(
'min' => '2018-12-06T09:00:00Z'
), array(
'min' => 'min',
));
try
{
$v->clean('2018-12-06T09:59:59+01:00');
$t->fail('->clean() throws an exception if the normalized date is before normalized min-option');
}
catch (sfValidatorError $e)
{
$t->pass('->clean() throws an exception if the normalized date is before normalized min-option');
$t->is($e->getMessage(), 'min', '->clean() adds correct min-message');
}
$v = new sfValidatorDateTime(array(
'max' => '2018-12-06T09:00:00Z'
), array(
'max' => 'max',
));
try
{
$v->clean('2018-12-06T10:00:01+01:00');
$t->fail('->clean() throws an exception if the normalized date is after normalized max-option');
}
catch (sfValidatorError $e)
{
$t->pass('->clean() throws an exception if the normalized date is after normalized max-option');
$t->is($e->getMessage(), 'max', '->clean() adds correct max-message');
}