You are here

public function DateObject::arrayErrors in Date 7

Same name and namespace in other branches
  1. 7.3 date_api/date_api.module \DateObject::arrayErrors()
  2. 7.2 date_api/date_api.module \DateObject::arrayErrors()
2 calls to DateObject::arrayErrors()
DateObject::parse in date_api/date_api.module
DateObject::__construct in date_api/date_api.module
Overridden constructor.

File

date_api/date_api.module, line 589
This module will make the date API available to other modules. Designed to provide a light but flexible assortment of functions and constants, with more functionality in additional files that are not loaded unless other modules specifically include them.

Class

DateObject
Extend PHP DateTime class with granularity handling, merge functionality and slightly more flexible initialization parameters.

Code

public function arrayErrors($arr) {
  $errors = array();
  $now = date_now();
  $default_month = !empty($arr['month']) ? $arr['month'] : $now
    ->format('n');
  $default_year = !empty($arr['year']) ? $arr['year'] : $now
    ->format('Y');
  foreach ($arr as $part => $value) {

    // Avoid false errors when a numeric value is input as a string by forcing it numeric.
    $value = intval($value);
    if (!empty($value) && $this
      ->forceValid($part, $value, 'now', $default_month, $default_year) != $value) {

      // Use a switchcase to make translation easier by providing a different message for each part.
      switch ($part) {
        case 'year':
          $errors['year'] = t('The year is invalid.');
          break;
        case 'month':
          $errors['month'] = t('The month is invalid.');
          break;
        case 'day':
          $errors['day'] = t('The day is invalid.');
          break;
        case 'hour':
          $errors['hour'] = t('The hour is invalid.');
          break;
        case 'minute':
          $errors['minute'] = t('The minute is invalid.');
          break;
        case 'second':
          $errors['second'] = t('The second is invalid.');
          break;
      }
    }
  }
  return $errors;
}