You are here

function date_formats in Date 5

1 call to date_formats()
date_set_formats in ./date.module

File

./date.inc, line 1690
Date/time API functions

Code

function date_formats($format, $granularity) {

  //split date part and time part
  preg_match("/([djDlzwWmnFMYy].*[djDlzwWmnFMYy])/", $format, $matches, PREG_OFFSET_CAPTURE);
  $format_date = $matches[1][0];
  $format_date_pos = $matches[1][1];
  preg_match("/([gGhHisaA].*[gGhHisaA])/", $format, $matches, PREG_OFFSET_CAPTURE);
  $format_time = $matches[1][0];
  $format_time_pos = $matches[1][1];
  $format_separator = substr($format, $format_date_pos + strlen($format_date), $format_time_pos - ($format_date_pos + strlen($format_date)));
  $jscal_format_date = $jscal_format_time = array();
  $text_format_date = $text_format_time = array();
  $select_format_date = array();
  $weight_index = 0;
  $desc_date = array();
  $desc_time = array();
  $format_to_jscalendar = array(
    'd' => '%d',
    'j' => '%e',
    'D' => '%a',
    'l' => '%A',
    'z' => '%j',
    'w' => '%w',
    'W' => '%U',
    'm' => '%m',
    'n' => '%m',
    'F' => '%B',
    'M' => '%b',
    'Y' => '%Y',
    'y' => '%y',
    'g' => '%l',
    'G' => '%k',
    'h' => '%I',
    'H' => '%H',
    'i' => '%M',
    's' => '%S',
    'a' => '%P',
    'A' => '%p',
  );
  foreach (preg_split('//', $format_date, -1, PREG_SPLIT_NO_EMPTY) as $c) {
    switch ($c) {
      case 'd':
      case 'j':
        if (in_array('D', $granularity)) {
          $jscal_format_date[] = $format_to_jscalendar[$c];
          $text_format_date[] = $c;
          $select_format_date['D'] = $weight_index++;
        }
        if ($c == 'd') {
          $desc_date[] = 'DD';
        }
        if ($c == 'j') {
          $desc_date[] = 'D';
        }
        break;
      case 'F':
      case 'M':
      case 'm':
      case 'n':
        if (in_array('M', $granularity)) {
          $jscal_format_date[] = $format_to_jscalendar[$c];
          $text_format_date[] = $c;
          $select_format_date['M'] = $weight_index++;
        }
        if ($c == 'M') {
          $desc_date[] = t('Jan');
        }
        if ($c == 'm') {
          $desc_date[] = 'MM';
        }
        if ($c == 'n') {
          $desc_date[] = 'M';
        }
        if ($c == 'F') {
          $desc_date[] = t('January');
        }
        break;
      case 'Y':
      case 'y':
        if (in_array('Y', $granularity)) {
          $jscal_format_date[] = $format_to_jscalendar[$c];
          $text_format_date[] = $c;
          $select_format_date['Y'] = $weight_index++;
        }
        if ($c == 'Y') {
          $desc_date[] = 'YYYY';
        }
        if ($c == 'y') {
          $desc_date[] = 'YY';
        }
        break;
      default:
        if (!preg_match('/[a-zA-Z]/', $c) && !isset($date_separator)) {
          $date_separator = $c;
        }
        break;
    }
  }
  if (date_has_time($granularity)) {
    foreach (preg_split('//', $format_time, -1, PREG_SPLIT_NO_EMPTY) as $c) {
      switch ($c) {
        case 'g':
        case 'G':
        case 'h':
        case 'H':
          if (in_array('H', $granularity)) {
            $jscal_format_time[] = $format_to_jscalendar[$c];
            $text_format_time[] = $c;
          }
          if ($c == 'g' || $c == 'h') {
            $desc_time[] = 'H';
          }
          if ($c == 'G' || $c == 'H') {
            $desc_time[] = 'HH';
          }
          break;
        case 'i':
          if (in_array('N', $granularity)) {
            $jscal_format_time[] = $format_to_jscalendar[$c];
            $text_format_time[] = $c;
          }
          $desc_time[] = 'MM';
          break;
        case 's':
          if (in_array('S', $granularity)) {
            $jscal_format_time[] = $format_to_jscalendar[$c];
            $text_format_time[] = $c;
          }
          $desc_time[] = 'SS';
          break;
        case 'a':
        case 'A':
          if (date_has_time($granularity)) {
            $jscal_format_ampm = $format_to_jscalendar[$c];
            $text_format_ampm = $c;
            $am_pm = true;
          }
          if ($c == 'a') {
            $desc_time[] = 'am';
          }
          if ($c == 'A') {
            $desc_time[] = 'AM';
          }
          break;
        default:
          if (!preg_match('/[a-zA-Z]/', $c) & !isset($time_separator)) {
            $time_separator = $c;
          }
          break;
      }
    }
  }

  // we store the current site-wide value, in order to be able to detect its changes
  if ($format == 'site-wide' || $format == variable_get('date_format_short', 'm/d/Y - H:i')) {
    $formats['input']['site-wide'] = $format;
  }
  $formats['input']['jscal'] = implode($date_separator, $jscal_format_date);
  $formats['input']['jscal'] .= date_has_time($granularity) ? $format_separator . implode($time_separator, $jscal_format_time) . $jscal_format_ampm : '';
  $formats['input']['text'] = implode($date_separator, $text_format_date);
  $formats['input']['text'] .= date_has_time($granularity) ? $format_separator . implode($time_separator, $text_format_time) . $text_format_ampm : '';
  $formats['input']['select'] = $select_format_date;
  $formats['input']['am_pm'] = $am_pm;
  $formats['input']['desc'] = implode($date_separator, $desc_date);
  $formats['input']['desc'] .= date_has_time($granularity) ? $format_separator . implode($time_separator, $desc_time) : '';
  return $formats;
}