format_date

Versions
4.6 – 5
format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL)
6 – 7
format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)

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.

$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

▾ 42 functions call 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_admin_overview in modules/comment/comment.admin.inc
Form builder; Builds the comment overview form for the admin.
comment_form in modules/comment/comment.module
Generate the basic commenting form, for appending to a node or display on a separate page.
dblog_event in modules/dblog/dblog.admin.inc
Menu callback; displays details about a log message.
dblog_overview in modules/dblog/dblog.admin.inc
Menu callback; displays a listing of log messages.
expand_date in includes/form.inc
Roll out a single date element.
filter_example_filter in developer/examples/filter_example.module
Implementation of hook_filter().
format_date in includes/common.inc
Format a date with the given configured format or a custom format string.
hook_watchdog in developer/hooks/core.php
Log an event message
map_month in includes/form.inc
Helper function for usage with drupal_map_assoc to display month names.
node_form in modules/node/node.pages.inc
Generate the node add/edit form array.
node_object_prepare in modules/node/node.pages.inc
node_revision_delete_confirm in modules/node/node.pages.inc
node_revision_delete_confirm_submit in modules/node/node.pages.inc
node_revision_overview in modules/node/node.pages.inc
Generate an overview table of older revisions of a node.
node_revision_revert_confirm in modules/node/node.pages.inc
Ask for confirmation of the reversion to prevent against CSRF attacks.
node_revision_revert_confirm_submit in modules/node/node.pages.inc
node_show in modules/node/node.module
Generate a page displaying a single node, along with its comments.
phptemplate_comment_submitted in themes/garland/template.php
phptemplate_node_submitted in themes/garland/template.php
statistics_access_log in modules/statistics/statistics.admin.inc
Menu callback; Displays recent page accesses.
statistics_node_tracker in modules/statistics/statistics.pages.inc
statistics_recent_hits in modules/statistics/statistics.admin.inc
Menu callback; presents the "recent hits" page.
statistics_user_tracker in modules/statistics/statistics.pages.inc
system_date_time_lookup in modules/system/system.admin.inc
Return the date for a given format string via Ajax.
system_date_time_settings in modules/system/system.admin.inc
Form builder; Configure the site date and time settings.
template_preprocess_aggregator_item in modules/aggregator/aggregator.pages.inc
Process variables for aggregator-item.tpl.php.
template_preprocess_comment in modules/comment/comment.module
Process variables for comment.tpl.php.
template_preprocess_comment_folded in modules/comment/comment.module
Process variables for comment-folded.tpl.php.
template_preprocess_node in includes/theme.inc
Process variables for node.tpl.php
template_preprocess_search_result in modules/search/search.pages.inc
Process variables for search-result.tpl.php.
theme_comment_submitted in modules/comment/comment.module
Theme a "Submitted by ..." notice.
theme_node_submitted in modules/node/node.module
Format the "Submitted by username on date/time" for each node
theme_update_report in modules/update/update.report.inc
Theme project status report.
theme_update_version in modules/update/update.report.inc
Theme the version display of a project.
user_mail_tokens in modules/user/user.module
Return an array of token to value mappings for user e-mail messages.
user_pass_reset in modules/user/user.pages.inc
Menu callback; process one time login link and redirects to the user page on success.
_blogapi_mt_extra in modules/blogapi/blogapi.module
Handles extra information sent by clients according to MovableType's spec.
_system_zonelist in modules/system/system.module
Generate an array of time zones and their local time&date.

Code

includes/common.inc, line 1301

<?php
function format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = 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('AaDlM', $c) !== FALSE) {
      $date .= t(gmdate($c, $timestamp), array(), $langcode);
    }
    else if ($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 '. gmdate($c, $timestamp), array('!long-month-name' => ''), $langcode));
    }
    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, $langcode);
    }
    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;
}
?>

More information on $format

NoCoolNamesRemain - Wed, 2009-09-23 13:31

"A PHP date format string as required by date()."
http://ca3.php.net/manual/en/function.date.php

Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.