public function testGetValue() { $_POST['form'] = 'datefield'; $_POST['date'] = '12/10/2009'; $this->assertEquals('12/10/2009', $this->txtDate->getValue()); $_POST['date'] = array('foo', 'bar'); $this->assertEquals('Array', $this->txtDate->getValue()); }
/** * Checks if this field is correctly submitted. * * @param string $error The error message to set. * @return bool */ public function isValid($error = null) { // call parent (let them do the hard word) $return = parent::isValid($error); // already errors detect, no more further testing is needed if ($return === false) { return false; } // define long mask $longMask = str_replace(array('d', 'm', 'y', 'Y'), array('dd', 'mm', 'yy', 'yyyy'), $this->mask); // post/get data $data = $this->getMethod(true); // init some vars $year = strpos($longMask, 'yyyy') !== false ? substr($data[$this->attributes['name']], strpos($longMask, 'yyyy'), 4) : substr($data[$this->attributes['name']], strpos($longMask, 'yy'), 2); $month = substr($data[$this->attributes['name']], strpos($longMask, 'mm'), 2); $day = substr($data[$this->attributes['name']], strpos($longMask, 'dd'), 2); // validate datefields that have a from-date set if (strpos($this->attributes['class'], 'inputDatefieldFrom') !== false) { // process from date $fromDateChunks = explode('-', $this->attributes['data-startdate']); $fromDateTimestamp = mktime(12, 00, 00, $fromDateChunks[1], $fromDateChunks[2], $fromDateChunks[0]); // process given date $givenDateTimestamp = mktime(12, 00, 00, $month, $day, $year); // compare dates if ($givenDateTimestamp < $fromDateTimestamp) { if ($error !== null) { $this->setError($error); } return false; } } elseif (strpos($this->attributes['class'], 'inputDatefieldTill') !== false) { // process till date $tillDateChunks = explode('-', $this->attributes['data-enddate']); $tillDateTimestamp = mktime(12, 00, 00, $tillDateChunks[1], $tillDateChunks[2], $tillDateChunks[0]); // process given date $givenDateTimestamp = mktime(12, 00, 00, $month, $day, $year); // compare dates if ($givenDateTimestamp > $tillDateTimestamp) { if ($error !== null) { $this->setError($error); } return false; } } elseif (strpos($this->attributes['class'], 'inputDatefieldRange') !== false) { // process from date $fromDateChunks = explode('-', $this->attributes['data-startdate']); $fromDateTimestamp = mktime(12, 00, 00, $fromDateChunks[1], $fromDateChunks[2], $fromDateChunks[0]); // process till date $tillDateChunks = explode('-', $this->attributes['data-enddate']); $tillDateTimestamp = mktime(12, 00, 00, $tillDateChunks[1], $tillDateChunks[2], $tillDateChunks[0]); // process given date $givenDateTimestamp = mktime(12, 00, 00, $month, $day, $year); // compare dates if ($givenDateTimestamp < $fromDateTimestamp || $givenDateTimestamp > $tillDateTimestamp) { if ($error !== null) { $this->setError($error); } return false; } } /** * When the code reaches the point, it means no errors have occurred * and truth will out! */ return true; }
/** * Get the UTC timestamp for a date/time object combination. * * @param \SpoonFormDate $date An instance of \SpoonFormDate. * @param \SpoonFormTime $time An instance of \SpoonFormTime. * @return int * @throws \Exception If provided $date, $time or both are invalid */ public static function getUTCTimestamp(\SpoonFormDate $date, \SpoonFormTime $time = null) { // validate date/time object if (!$date->isValid() || $time !== null && !$time->isValid()) { throw new \Exception('You need to provide two objects that actually contain valid data.'); } // init vars $year = gmdate('Y', $date->getTimestamp()); $month = gmdate('m', $date->getTimestamp()); $day = gmdate('j', $date->getTimestamp()); if ($time !== null) { // define hour & minute list($hour, $minute) = explode(':', $time->getValue()); } else { // user default time $hour = 0; $minute = 0; } // make and return timestamp return mktime($hour, $minute, 0, $month, $day, $year); }