public function setGtuToDate($fd) { if ($fd instanceof FuzzyDateTime) { $this->_set('gtu_to_date', $fd->format('Y/m/d H:i:s')); $this->_set('gtu_to_date_mask', $fd->getMask()); } else { $dateTime = new FuzzyDateTime($fd, 56, false, true); if (is_array($fd)) { $dateTime->setMask(FuzzyDateTime::getMaskFromDate($fd)); } $this->_set('gtu_to_date', $dateTime->format('Y/m/d H:i:s')); $this->_set('gtu_to_date_mask', $dateTime->getMask()); } }
/** * @param array|string $value Date/Time passed as an array or a string * @var string $checkDateStructure: Get an empty string if the date structure is correct, otherwise get the error * @var FuzzyDateTime $clean: A FuzzyDateTime object containing the date/time corrected and the associated mask * @return FuzzyDateTime $clean * @see sfValidatorBase */ protected function doClean($value) { if (is_array($value)) { try { // Check date time structure $checkDateStructure = FuzzyDateTime::checkDateTimeStructure($value); } catch (Exception $e) { throw new sfValidatorError($this, 'invalid', array('value' => $value)); } if (!empty($checkDateStructure)) { throw new sfValidatorError($this, $checkDateStructure, array('value' => $value)); } try { $clean = new FuzzyDateTime($value, FuzzyDateTime::getMaskFromDate($value), $this->getOption('from_date'), $this->getOption('with_time')); } catch (Exception $e) { throw new sfValidatorError($this, 'invalid', array('value' => $value)); } } else { if ($regex = $this->getOption('date_format')) { // ...and that it can be extracted from string if (!preg_match($regex, $value, $match)) { throw new sfValidatorError($this, 'bad_format', array('value' => $value, 'date_format' => $this->getOption('date_format_error') ? $this->getOption('date_format_error') : $this->getOption('date_format'))); } $clean = new FuzzyDateTime($match); } else { try { $clean = new FuzzyDateTime($value); } catch (Exception $e) { throw new sfValidatorError($this, 'invalid', array('value' => $value)); } } } // Check date is between min and max values given if ($this->hasOption('max') && $clean > $this->getOption('max')) { throw new sfValidatorError($this, 'max', array('value' => $value, 'max' => $this->getOption('max'))); } if ($this->hasOption('min') && $clean < $this->getOption('min')) { throw new sfValidatorError($this, 'min', array('value' => $value, 'min' => $this->getOption('min'))); } return $clean; }
$testArray['day'] = '24'; $t->is(FuzzyDateTime::getMaskFromDate($testArray), 63, 'The Mask returned by getMaskFromDate is correct !'); $fdt->setMaskFromDate($testArray); $t->is($fdt->getMask(), 63, 'Setting of mask from date array worked !'); $testArray['second'] = ''; $t->is(FuzzyDateTime::getMaskFromDate($testArray), 62, 'The Mask returned by getMaskFromDate is correct !'); $testArray['minute'] = ''; $t->is(FuzzyDateTime::getMaskFromDate($testArray), 60, 'The Mask returned by getMaskFromDate is correct !'); $testArray['hour'] = ''; $t->is(FuzzyDateTime::getMaskFromDate($testArray), 56, 'The Mask returned by getMaskFromDate is correct !'); $testArray['day'] = ''; $t->is(FuzzyDateTime::getMaskFromDate($testArray), 48, 'The Mask returned by getMaskFromDate is correct !'); $testArray['month'] = ''; $t->is(FuzzyDateTime::getMaskFromDate($testArray), 32, 'The Mask returned by getMaskFromDate is correct !'); $testArray['year'] = ''; $t->is(FuzzyDateTime::getMaskFromDate($testArray), 0, 'The Mask returned by getMaskFromDate is correct !'); $fdt->setDateFormat('d/m/Y'); $fdt->setTimeFormat('H:i:s'); $fdt->setWithTime(true); $fdt->setMask(0); $t->is($fdt->getDateMasked(), '<em>24/02/1975 13:12:11</em>', 'The date displayed is well with 0 as mask second mask'); $fdt->setMask(1); $t->is($fdt->getDateMasked(), '<em>24/02/1975 13:12</em>:11', 'The date displayed is well with second mask only'); $fdt->setMask(2); $t->is($fdt->getDateMasked(), '<em>24/02/1975 13</em>:12:<em>11</em>', 'The date displayed is well with minute only'); $fdt->setMask(4); $t->is($fdt->getDateMasked(), '<em>24/02/1975</em> 13:<em>12:11</em>', 'The date displayed is well with hours only'); $fdt->setMask(8); $t->is($fdt->getDateMasked(), '24/<em>02/1975 13:12:11</em>', 'The date displayed is well with days only'); $fdt->setMask(16); $t->is($fdt->getDateMasked(), '<em>24</em>/02/<em>1975 13:12:11</em>', 'The date displayed is well with month only');