You are here

function format_date in Drupal 5

Same name and namespace in other branches
  1. 8 core/includes/common.inc \format_date()
  2. 4 includes/common.inc \format_date()
  3. 6 includes/common.inc \format_date()
  4. 7 includes/common.inc \format_date()

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 offset in seconds; if omitted, the user's time zone is used.

Return value

A translated date string in the requested format.

Related topics

34 calls to format_date()
blogapi_blogger_edit_post in modules/blogapi/blogapi.module
Blogging API callback. Modifies the specified blog node.
blogapi_blogger_new_post in modules/blogapi/blogapi.module
Blogging API callback. Inserts a new blog post as a node.
chameleon_comment in themes/chameleon/chameleon.theme
chameleon_node in themes/chameleon/chameleon.theme
comment.tpl.php in themes/garland/comment.tpl.php

... See full list

File

includes/common.inc, line 1209
Common functions that many Drupal modules will need to reference.

Code

function format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL) {
  if (!isset($timezone)) {
    global $user;
    if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
      $timezone = $user->timezone;
    }
    else {
      $timezone = variable_get('date_default_timezone', 0);
    }
  }
  $timestamp += $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 = '';
  for ($i = 0; $i < $max; $i++) {
    $c = $format[$i];
    if (strpos('AaDFlM', $c) !== FALSE) {
      $date .= t(gmdate($c, $timestamp));
    }
    else {
      if (strpos('BdgGhHiIjLmnsStTUwWYyz', $c) !== FALSE) {
        $date .= gmdate($c, $timestamp);
      }
      else {
        if ($c == 'r') {
          $date .= format_date($timestamp - $timezone, 'custom', 'D, d M Y H:i:s O', $timezone);
        }
        else {
          if ($c == 'O') {
            $date .= sprintf('%s%02d%02d', $timezone < 0 ? '-' : '+', abs($timezone / 3600), abs($timezone % 3600) / 60);
          }
          else {
            if ($c == 'Z') {
              $date .= $timezone;
            }
            else {
              if ($c == '\\') {
                $date .= $format[++$i];
              }
              else {
                $date .= $c;
              }
            }
          }
        }
      }
    }
  }
  return $date;
}