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 'short', 'medium' and 'long' 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 "short", "medium" or "long" 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

▾ 43 functions call format_date()

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.
comment_tokens in modules/comment/comment.tokens.inc
Implement hook_tokens().
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.
filter_example_filter in developer/examples/filter_example.module
Implementation of hook_filter().
form_process_date in includes/form.inc
Roll out a single date element.
hook_watchdog in modules/system/system.api.php
Log an event message
locale_date_format_form in modules/locale/locale.module
Provide date localization configuration options to users.
node_admin_nodes in modules/node/node.admin.inc
Form builder: Builds the node administration overview.
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 an array which displays a node detail page.
node_tokens in modules/node/node.tokens.inc
Implement hook_tokens().
poll_votes in modules/poll/poll.pages.inc
Callback for the 'votes' tab for polls you can see other votes on
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_tokens in modules/statistics/statistics.tokens.inc
Implement hook_tokens().
statistics_user_tracker in modules/statistics/statistics.pages.inc
system_add_date_format_type_form in modules/system/system.admin.inc
Add new date type.
system_configure_date_formats_form in modules/system/system.admin.inc
Allow users to add additional date formats.
system_date_delete_format_form in modules/system/system.admin.inc
Menu callback; present a form for deleting a date format.
system_date_delete_format_form_submit in modules/system/system.admin.inc
Delete a configured date format.
system_date_time_formats in modules/system/system.admin.inc
Displays the date format strings overview page.
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.
system_time_zones in modules/system/system.module
Generate an array of time zones and their local time&date.
system_tokens in modules/system/system.tokens.inc
Implement hook_tokens().
system_token_info in modules/system/system.tokens.inc
Implement hook_token_info().
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_node in modules/node/node.module
Process variables for node.tpl.php
template_preprocess_search_result in modules/search/search.pages.inc
Process variables for search-result.tpl.php.
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_pass_reset in modules/user/user.pages.inc
Menu callback; process one time login link and redirects to the user page on success.
user_tokens in modules/user/user.tokens.inc
Implement hook_tokens().

Code

includes/common.inc, line 2276

<?php
function format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
  // Use the advanced drupal_static() pattern, since this is called very often.
  static $drupal_static = array();
  isset($drupal_static[__FUNCTION__]) || ($drupal_static[__FUNCTION__] = &drupal_static(__FUNCTION__));
  $timezones = &$drupal_static[__FUNCTION__];

  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);
  }

  // Use the default langcode if none is set.
  global $language;
  if (empty($langcode)) {
    $langcode = isset($language->language) ? $language->language : 'en';
  }

  switch ($type) {
    case 'short':
      $format = variable_get('date_format_short', 'm/d/Y - H:i');
      break;
    case 'long':
      $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');
  }

  // 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]);

  // Encode markers that should be translated. 'A' becomes '\xEF\AA\xFF'.
  // xEF and xFF are invalid UTF-8 sequences, and we assume they are not in the
  // input string.
  // Paired backslashes are isolated to prevent errors in read-ahead evaluation.
  // The read-ahead expression ensures that A matches, but not \A.
  $format = preg_replace(array('/\\\\\\\\/', '/(?<!\\\\)([AaeDlMTF])/'), array("\xEF\\\\\\\\\xFF", "\xEF\\\\\$1\$1\xFF"), $format);

  // Call date_format().
  $format = date_format($date_time, $format);

  // Pass the langcode to _format_date_callback().
  _format_date_callback(NULL, $langcode);

  // Translate the marked sequences.
  return preg_replace_callback('/\xEF([AaeDlMTF]?)(.*?)\xFF/', '_format_date_callback', $format);
}
?>
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.