1 date.inc date_format_date($date, $type = 'medium', $format = '', $langcode = NULL)

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()

File

core/includes/date.inc, line 629
Date API functions.

Code

function date_format_date($date, $type = 'medium', $format = '', $langcode = NULL) {
  if (empty($date)) {
    return '';
  }
  if ($type != 'custom') {
    //check that we have an entry in system.date.json
    $this_format = system_date_format_load($type, $include_hidden = TRUE);
    if (isset($this_format)) {
      $format = $this_format['pattern'];
    }
    else {
      $this_format = system_date_format_load('medium');
      $format = $this_format['pattern'];
    }
  }
  $format = date_limit_format($format, $date->granularity);
  $max = strlen($format);
  $date_string = '';
  for ($i = 0; $i < $max; $i++) {
    $c = $format[$i];
    switch ($c) {
      case 'l':
        $date_string .= t($date->format('l'), array(), array('context' => '', 'langcode' => $langcode));
        break;

      case 'D':
        $date_string .= t($date->format('D'), array(), array('context' => '', 'langcode' => $langcode));
        break;

      case 'F':
        $date_string .= t($date->format('F'), array(), array('context' => 'Long month name', 'langcode' => $langcode));
        break;

      case 'M':
        $date_string .= t($date->format('M'), array(), array('langcode' => $langcode));
        break;

      case 'A':
      case 'a':
        $date_string .= t($date->format($c), array(), array('context' => 'ampm', 'langcode' => $langcode));
        break;

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

        // Remaining date parts need no translation.
      case 'O':
        $date_string .= 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':
        $date_string .= 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':
        $date_string .= date_offset_get($date);
        break;

      case '\\':
        $date_string .= $format[++$i];
        break;

      case 'r':
        $date_string .= date_format_date($date, 'custom', 'D, d M Y H:i:s O', 'en');
        break;

      default:
        // cspell:disable-next-line
        if (strpos('BdcgGhHiIjLmnNosStTuUwWYyz', $c) !== FALSE) {
          $date_string .= $date->format($c);
        }
        else {
          $date_string .= $c;
        }
    }
  }
  return $date_string;
}