You are here

function date_format_date in Date 7.2

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 date_api/date_api.module \date_format_date()

Formats a date, using a date type or a custom date format string.

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

object $date: A date object.

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

string $format: (optional) 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. Defaults to an empty string.

string $langcode: (optional) Language code to translate to. Defaults to NULL.

Return value

string A translated date string in the requested format.

See also

format_date()

20 calls to date_format_date()
DateApiTestCase::testDateApi in date_api/tests/DateApiTestCase.test
Test date_format_date().
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_format_type_options in date_api/date_api.module
Creates an array of date format types for use as an options list.
date_popup_element_value_callback in date_popup/date_popup.module
Element value callback for date_popup element.

... See full list

File

date_api/date_api.module, line 1788
This module will make the date API available to other modules.

Code

function date_format_date($date, $type = 'medium', $format = '', $langcode = NULL) {
  if (empty($date)) {
    return '';
  }
  if ($type != 'custom') {
    $format = variable_get('date_format_' . $type);
  }
  if ($type != 'custom' && empty($format)) {
    $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' => '',
          'langcode' => $langcode,
        ));
        break;
      case 'D':
        $datestring .= t($date
          ->format('D'), array(), array(
          'context' => '',
          'langcode' => $langcode,
        ));
        break;
      case 'F':
        $datestring .= t($date
          ->format('F'), array(), array(
          'context' => 'Long month name',
          'langcode' => $langcode,
        ));
        break;
      case 'M':
        $datestring .= t($date
          ->format('M'), array(), array(
          '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', 'en');
        break;
      default:
        if (strpos('BdcgGhHiIjLmnNosStTuUwWYyz', $c) !== FALSE) {
          $datestring .= $date
            ->format($c);
        }
        else {
          $datestring .= $c;
        }
    }
  }
  return $datestring;
}