/** * Compares whether or not a date is some number of days | months | years after a date * This is a magic method and can be called via daysInFuture, monthsInFuture and yearsInFuture * * @param object $model * @param string $method the name of hte magic method * @param array $check the data of the field to be checked * @param integer $value of days | months | years that $check should be in the future * @param array $params [optional] * - 'fields' array of fields that should be matched against (default: array()) * - 'timezone' string timezone identifier (default: 'UTC') * @return boolean if $check is at least $value days | months | years in the future * @access public * @author Jose Diaz-Gonzales * @author Thomas Ploch * @link http://snipplr.com/view/2223/get-number-of-days-between-two-dates/ */ function inFuture(&$model, $method, $check, $value, $params = array()) { $valid = false; // If $check is not a valid date if (!Validation::date(reset($check), 'Y-m-d')) { return $valid; } // Get the $mode from method name $mode = str_replace('infuture', '', $method); /* PHP5 * $mode = str_replace(low(__METHOD__), '', $method); */ // Default config $defaultConfig = array('fields' => array(), 'timezone' => 'UTC'); // Get options extract(am($defaultConfig, $params)); if (empty($fields)) { return $valid; } // Setting the timezone if possible if (function_exists('date_default_timezone_set')) { date_default_timezone_set($timezone); } /* * TODO: add cases for months and years to switch * FIXME: refactor cases into helper functions */ switch ($mode) { case 'days': foreach ($fields as $field) { // First we need to break these dates into their constituent parts: $gd_a = getdate(strtotime($model->data[$model->alias][$field])); $gd_b = getdate(strtotime(reset($check))); // Now recreate these timestamps, based upon noon on each day // The specific time doesn't matter but it must be the same each day $a_new = mktime(12, 0, 0, $gd_a['mon'], $gd_a['mday'], $gd_a['year']); $b_new = mktime(12, 0, 0, $gd_b['mon'], $gd_b['mday'], $gd_b['year']); // Subtract these two numbers and divide by the number of seconds in a // day. Round the result since crossing over a daylight savings time // barrier will cause this time to be off by an hour or two. $valid = round(abs($a_new - $b_new) / 86400) >= $params['days']; if (!$valid) { return $valid; } } return $valid; default: return $valid; } }
/** * checkDatetimeType * * @param array $question question * @param string $answer answer value * @return array error message */ public function checkDatetimeType($question, $answer) { $errors = array(); if ($question['question_type_option'] == QuestionnairesComponent::TYPE_OPTION_DATE) { if (!Validation::date($answer, 'ymd')) { $errors[] = sprintf(__d('questionnaires', 'Please enter a valid date in YY-MM-DD format.')); } } elseif ($question['question_type_option'] == QuestionnairesComponent::TYPE_OPTION_TIME) { if (!Validation::time($answer)) { $errors[] = sprintf(__d('questionnaires', 'Please enter the time.')); } } elseif ($question['question_type_option'] == QuestionnairesComponent::TYPE_OPTION_DATE_TIME) { if (!Validation::datetime($answer, 'ymd')) { $errors[] = sprintf(__d('questionnaires', 'Please enter a valid date and time.')); } } return $errors; }
/** * _validateDatetime 日付・時間の正当性 * * @param object &$model use model * @param int $questionTypeOption 時間・日付オプション * @param string $answer 登録データ * @return bool */ protected function _validateDatetime(&$model, $questionTypeOption, $answer) { if ($questionTypeOption == RegistrationsComponent::TYPE_OPTION_DATE) { if (!Validation::date($answer, 'ymd')) { $model->validationErrors['answer_value'][] = __d('registrations', 'Please enter a valid date in YY-MM-DD format.'); return false; } } elseif ($questionTypeOption == RegistrationsComponent::TYPE_OPTION_TIME) { if (!Validation::time($answer)) { $model->validationErrors['answer_value'][] = __d('registrations', 'Please enter the time.'); return false; } } elseif ($questionTypeOption == RegistrationsComponent::TYPE_OPTION_DATE_TIME) { if (!Validation::datetime($answer, 'ymd')) { $model->validationErrors['answer_value'][] = __d('registrations', 'Please enter a valid date and time.'); return false; } } else { $model->validationErrors['answer_value'][] = __d('net_commons', 'Invalid request.'); return false; } return true; }
/** * testDateCustomRegx method * * @return void */ public function testDateCustomRegx() { $this->assertTrue(Validation::date('2006-12-27', null, '%^(19|20)[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$%')); $this->assertFalse(Validation::date('12-27-2006', null, '%^(19|20)[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$%')); }
/** * Validates a datetime value by acting as a decorator for native * Validation::date() and Validation::time() methods. * * @param $check array field_name => value * @param $options array Options for this rule * @return boolean * @access public */ public function datetime($check, $options) { $check = array_shift(array_values($check)); $datetime = strtotime($check); if ($datetime !== false) { return Validation::date(date('Y-m-d', $datetime), 'ymd') && Validation::time(date('H:i', $datetime)); } return false; }
/** * @testdox date should return true to valid date */ public function testValidDate() { $value = '01/01/2009'; $this->assertTrue(Validation::date($value)); }
/** * Validation of Date Fields (as the core one is buggy!!!) * @param options * - dateFormat (defaults to 'ymd') * - allowEmpty * - after/before (fieldName to validate against) * - min (defaults to 0 - equal is OK too) * 2011-03-02 ms */ public function validateDate($data, $options = array()) { $format = !empty($options['format']) ? $options['format'] : 'ymd'; if (is_array($data)) { $value = array_shift($data); } else { $value = $data; } $dateTime = explode(' ', trim($value), 2); $date = $dateTime[0]; if (!empty($options['allowEmpty']) && (empty($date) || $date == DEFAULT_DATE)) { return true; } if (Validation::date($date, $format)) { # after/before? $days = !empty($options['min']) ? $options['min'] : 0; if (!empty($options['after']) && isset($this->data[$this->alias][$options['after']])) { if ($this->data[$this->alias][$options['after']] > date(FORMAT_DB_DATE, strtotime($date) - $days * DAY)) { return false; } } if (!empty($options['before']) && isset($this->data[$this->alias][$options['before']])) { if ($this->data[$this->alias][$options['before']] < date(FORMAT_DB_DATE, strtotime($date) + $days * DAY)) { return false; } } return true; } return false; }
/** * __checkMinMaxDate * min and max is require both value * * @param object &$model use model * @param bool $check post data * @return bool */ private function __checkMinMaxDate(&$model, $check) { if ($model->data['QuestionnaireQuestion']['question_type_option'] == QuestionnairesComponent::TYPE_OPTION_DATE) { if (!Validation::date($model->data['QuestionnaireQuestion']['min'])) { return false; } if (!Validation::date($model->data['QuestionnaireQuestion']['max'])) { return false; } } return true; }
/** * Fonction permettant de tester si une date est valide * * @param varchar $date Date * @param varchar $format format * @return boolean * @access public * @author koéZionCMS * @version 0.1 - 16/07/2014 by FI * @version 0.2 - 21/07/2014 by FI - Rajout du format */ public function check_date($date, $format = 'ymd') { //On utilise la règle de validation prévue à cet effet require_once KOEZION . DS . 'validation.php'; $validation = new Validation(); return $validation->date($date, $format); }
public function testInvalidDate() { $value = "2009-01-01"; $this->assertFalse(Validation::date($value)); }
/** * formatDate * jpn: AdditionalValidationPatternsBehavior用にemptyのときはtrue * */ public function formatDate(Model $model, $field) { $value = array_shift($field); if (!Validation::notEmpty($value)) { return true; } return Validation::date($value); }