You are here

function event_format_date in Event 5.2

Format a date with the given configured format or a custom format string.

Drupal allows administrators to select formatting strings for 'small', 'medium' and 'large' date formats. This function can handle these formats, as well as any custom format.

This version of this function was adapted to be used with date strings of format: YYYY-MM-DD HH:mm:ss

Parameters

$date: The exact date to format, as a date array or string.

$type: The format to use. Can be "small", "medium" or "large" for the preconfigured date formats. If "custom" is specified, then $format is required as well.

$format: A PHP date format string as required by date(). A backslash should be used before a character to avoid interpreting the character as part of a date format. 'r', 'O', and 'Z' aren't implemented.

Return value

A translated date string in the requested format.

17 calls to event_format_date()
event_calendar_data in ./event.module
Returns an array of nodes that occur on a given date. Handles content type and taxonomy filters.
event_calendar_day in ./event.module
Displays a daily event calendar.
event_calendar_ical in ./event.module
Creates an ical feed of events.
event_calendar_month in ./event.module
Displays a monthly event calendar.
event_calendar_rss in ./event.module
Creates an rss feed of events.

... See full list

File

./event.module, line 2763

Code

function event_format_date($date, $type = 'medium', $format = '') {
  if (!is_array($date)) {
    $date = event_explode_date($date);
  }
  switch ($type) {
    case 'small':
      $format = variable_get('date_format_short', 'm/d/Y - H:i');
      break;
    case 'large':
      $format = variable_get('date_format_long', 'l, F j, Y - H:i');
      break;
    case 'custom':

      // No change to format
      break;
    case 'medium':
    default:
      $format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
  }
  $max = strlen($format);
  $ret = '';
  for ($i = 0; $i < $max; $i++) {
    $c = $format[$i];
    switch ($c) {
      case 'a':
        if ($date['hour'] < 12) {
          $ret .= t('am');
        }
        else {
          $ret .= t('pm');
        }
        break;
      case 'A':
        if ($date['hour'] < 12) {
          $ret .= t('AM');
        }
        else {
          $ret .= t('PM');
        }
        break;
      case 'Y':
        $ret .= $date['year'];
        break;
      case 'y':
        $ret .= substr($date['year'], 2);
        break;
      case 'F':
        $months = _event_months();
        $ret .= t($months[$date['month']]);
        break;
      case 'm':
        $ret .= $date['month'];
        break;
      case 'M':
        $months = _event_months_abbrev();
        $ret .= t($months[$date['month']]);
        break;
      case 'n':
        $ret .= (int) $date['month'];
        break;
      case 'd':
        $ret .= $date['day'];
        break;
      case 'j':
        $ret .= (int) $date['day'];
        break;
      case 'S':
        $values = array(
          '01' => t('st'),
          '02' => t('nd'),
          '03' => t('rd'),
        );
        if (isset($values[$date['day']])) {
          $ret .= $values[$date['day']];
          break;
        }
        $ret .= t('th');
        break;
      case 'g':
        $result = $date['hour'] % 12;
        if ($result == 0) {
          $result = 12;
        }
        $ret .= $result;
        break;
      case 'G':
        $ret .= (int) $date['hour'];
        break;
      case 'h':
        $result = $date['hour'] % 12;
        if ($result == 0) {
          $result = 12;
        }
        $ret .= str_pad($result, 2, '0', STR_PAD_LEFT);
        break;
      case 'H':
        $ret .= $date['hour'];
        break;
      case 'i':
        $ret .= $date['minute'];
        break;
      case 's':
        $ret .= $date['second'];
        break;
      case 'l':
        $ret .= t(gmdate('l', strtotime($date['year'] . '-' . $date['month'] . '-' . $date['day'] . ' 12:00:00')));
        break;
      case 'D':
        $ret .= t(gmdate('D', strtotime($date['year'] . '-' . $date['month'] . '-' . $date['day'] . ' 12:00:00')));
        break;
      case 'w':
        $ret .= t(gmdate('w', strtotime($date['year'] . '-' . $date['month'] . '-' . $date['day'] . ' 12:00:00')));
        break;
      case 't':
        $ret .= gmdate('t', strtotime($date['year'] . '-' . $date['month'] . '-' . $date['day'] . ' 12:00:00'));
        break;
      case 'r':
        $ret .= 'r is not implemented by event_format_date';
        break;
      case 'O':
        $ret .= 'O is not implemented by event_format_date';
        break;
      case 'Z':
        $ret .= 'Z is not implemented by event_format_date';
        break;
      case '\\':
        $ret .= $format[++$i];
        break;
      default:
        $ret .= $c;
    }
  }
  return $ret;
}