Definition

format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)
drupal-cvs/includes/common.inc, line 1458

Description

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.

Parameters

$timestamp The exact date to format, as a UNIX timestamp.

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

$timezone Time zone identifier; if omitted, the user's time zone is used.

$langcode Optional language code to translate to a language other than what is used to display the page.

Return value

A translated date string in the requested format.

Related topics

Namesort iconDescription
FormattingFunctions to format numbers, strings, dates, etc.

Code

<?php
function format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
  static $timezones = array();
  if (!isset($timezone)) {
    global $user;
    if (variable_get('configurable_timezones', 1) && $user->uid && $user->timezone) {
      $timezone = $user->timezone;
    }
    else {
      $timezone = variable_get('date_default_timezone', 'UTC');
    }
  }
  // Store DateTimeZone objects in an array rather than repeatedly
  // contructing identical objects over the life of a request.
  if (!isset($timezones[$timezone])) {
    $timezones[$timezone] = timezone_open($timezone);
  }

  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);
  $date = '';
  // Create a DateTime object from the timestamp.
  $date_time = date_create('@' . $timestamp);
  // Set the time zone for the DateTime object.
  date_timezone_set($date_time, $timezones[$timezone]);

  for ($i = 0; $i < $max; $i++) {
    $c = $format[$i];
    if (strpos('AaeDlMT', $c) !== FALSE) {
      $date .= t(date_format($date_time, $c), array(), $langcode);
    }
    elseif ($c == 'F') {
      // Special treatment for long month names: May is both an abbreviation
      // and a full month name in English, but other languages have
      // different abbreviations.
      $date .= trim(t('!long-month-name ' . date_format($date_time, $c), array('!long-month-name' => ''), $langcode));
    }
    elseif (strpos('BcdGgHhIijLmNnOoPSstUuWwYyZz', $c) !== FALSE) {
      $date .= date_format($date_time, $c);
    }
    elseif ($c == 'r') {
      $date .= format_date($timestamp, 'custom', 'D, d M Y H:i:s O', $timezone, $langcode);
    }
    elseif ($c == '\\') {
      $date .= $format[++$i];
    }
    else {
      $date .= $c;
    }
  }

  return $date;
}
?>