You are here

function partial_date_format_component in Partial Date 7

1 call to partial_date_format_component()
partial_date_format in ./partial_date.module

File

./partial_date.module, line 1659
Defines a date element that allows for any combination of date granularity settings.

Code

function partial_date_format_component($value, $format, &$date, $additional = array()) {
  $additional += array(
    'year_designation' => 'ce',
  );

  // If dealing with 12 hour times, recalculate the value.
  if ($format == 'h' || $format == 'g') {
    if ($value > 12) {
      $value -= 12;
    }
    elseif ($value == 0) {
      $value = '12';
    }
    if (empty($additional['meridiem'])) {
      $additional['meridiem'] = 'a';
    }
  }
  else {
    $additional['meridiem'] = '';
  }

  // Add suffixes for year and time formats
  $suffix = '';
  switch ($format) {
    case 'd-S':
    case 'j-S':
      $suffix = partial_date_day_ordinal_suffix($value);
      break;
    case 'y-ce':
    case 'Y-ce':
      $suffix = partial_date_year_designation_decorator($value, $additional['year_designation']);
      if (!empty($suffix) && !empty($value)) {
        $value = abs($value);
      }
      break;
  }
  switch ($format) {
    case 'y-ce':
    case 'y':
      return (strlen($value) > 2 ? substr($value, -2) : $value) . $suffix;
    case 'F':
      return partial_date_month_names($value) . $suffix;
    case 'M':
      return partial_date_month_abbreviations($value) . $suffix;

    // Numeric representation of the day of the week  0 (for Sunday) through 6 (for Saturday)
    case 'w':
      if (!empty($date['year']) && !empty($date['month'])) {
        return partial_date_day_of_week($date['year'], $date['month'], $value) . $suffix;
      }
      return '';

    // A full textual representation of the day of the week.
    case 'l':

    // A textual representation of a day, three letters.
    case 'D':
      if (!empty($date['year']) && !empty($date['month'])) {
        $day = partial_date_day_of_week($date['year'], $date['month'], $value);
        if ($format == 'D') {
          return partial_date_weekday_name_abbreviations($day, 3) . $suffix;
        }
        else {
          return partial_date_weekday_names($day) . $suffix;
        }
      }
      return '';
    case 'n':
    case 'j':
    case 'j-S':
    case 'g':
    case 'G':
      return intval($value) . $suffix;
    case 'd-S':
    case 'd':
    case 'h':
    case 'H':
    case 'i':
    case 's':
    case 'm':
      return sprintf('%02s', $value) . $suffix;
    case 'Y-ce':
    case 'Y':
    case 'e':
      return $value . $suffix;
    case 'T':
      try {
        $tz = new DateTimeZone($value);
        $transitions = $tz
          ->getTransitions();
        return $transitions[0]['abbr'] . $suffix;
      } catch (Exception $e) {
      }
      return '';

    // Todo: implement
    // Year types
    // ISO-8601 year number
    case 'o':

    // Day types
    // The day of the year
    case 'z':

    // ISO-8601 numeric representation of the day of the week
    case 'N':

    // Timezone offsets
    // Whether or not the date is in daylight saving time
    case 'I':

    // Difference to Greenwich time (GMT) in hours
    case 'O':

    // Difference to Greenwich time (GMT) with colon between hours and minutes
    case 'P':

    // Timezone offset in seconds
    case 'Z':
    default:
      return '';
  }
}