You are here

function date_format_date in Date 7

Same name and namespace in other branches
  1. 5.2 date_api.module \date_format_date()
  2. 5 date.inc \date_format_date()
  3. 6.2 date_api.module \date_format_date()
  4. 6 date_api.module \date_format_date()
  5. 7.3 date_api/date_api.module \date_format_date()
  6. 7.2 date_api/date_api.module \date_format_date()

Reworked from Drupal's format_date function to handle pre-1970 and post-2038 dates and accept a date object instead of a timestamp as input.

Translates formatted date results, unlike PHP function date_format(). Should only be used for display, not input, because it can't be parsed.

Parameters

$oject: A date object.

$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.

Return value

A translated date string in the requested format.

14 calls to date_format_date()
DateAPITestCase::testDateAPI in tests/date_api.test
date_entity_metadata_struct_getter in ./date.module
Getter callback to return date values as datestamp in UTC.
date_formatter_process in ./date.module
Helper function for creating formatted date arrays from a formatter.
date_popup_process_date in date_popup/date_popup.module
Process the date portion of the element.
date_popup_process_time in date_popup/date_popup.module
Process the time portion of the element.

... See full list

File

date_api/date_api.module, line 1177
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.

Code

function date_format_date($date, $type = 'medium', $format = '', $langcode = NULL) {
  if (empty($date)) {
    return '';
  }
  switch ($type) {
    case 'small':
    case 'short':
      $format = variable_get('date_format_short', 'm/d/Y - H:i');
      break;
    case 'large':
    case 'long':
      $format = variable_get('date_format_long', 'l, F j, Y - H:i');
      break;
    case 'custom':
      $format = $format;
      break;
    case 'medium':
    default:
      $format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
  }
  $format = date_limit_format($format, $date->granularity);
  $max = strlen($format);
  $datestring = '';
  for ($i = 0; $i < $max; $i++) {
    $c = $format[$i];
    switch ($c) {
      case 'l':
        $datestring .= t($date
          ->format('l'), array(), array(
          'context' => 'day_name',
          'langcode' => $langcode,
        ));
        break;
      case 'D':
        $datestring .= t($date
          ->format('D'), array(), array(
          'context' => 'day_abbr',
          'langcode' => $langcode,
        ));
        break;
      case 'F':
        $datestring .= t($date
          ->format('F'), array(), array(
          'context' => 'month_name',
          'langcode' => $langcode,
        ));
        break;
      case 'M':
        $datestring .= t($date
          ->format('M'), array(), array(
          'context' => 'month_abbr',
          'langcode' => $langcode,
        ));
        break;
      case 'A':
      case 'a':
        $datestring .= t($date
          ->format($c), array(), array(
          'context' => 'ampm',
          'langcode' => $langcode,
        ));
        break;

      // The timezone name translations can use t().
      case 'e':
      case 'T':
        $datestring .= t($date
          ->format($c));
        break;

      // Remaining date parts need no translation.
      case 'O':
        $datestring .= sprintf('%s%02d%02d', date_offset_get($date) < 0 ? '-' : '+', abs(date_offset_get($date) / 3600), abs(date_offset_get($date) % 3600) / 60);
        break;
      case 'P':
        $datestring .= sprintf('%s%02d:%02d', date_offset_get($date) < 0 ? '-' : '+', abs(date_offset_get($date) / 3600), abs(date_offset_get($date) % 3600) / 60);
        break;
      case 'Z':
        $datestring .= date_offset_get($date);
        break;
      case '\\':
        $datestring .= $format[++$i];
        break;
      case 'r':
        $datestring .= date_format_date($date, 'custom', 'D, d M Y H:i:s O', $langcode);
        break;
      default:
        if (strpos('BdcgGhHiIjLmnNosStTuUwWYyz', $c) !== FALSE) {
          $datestring .= $date
            ->format($c);
        }
        else {
          $datestring .= $c;
        }
    }
  }
  return $datestring;
}