Community Documentation

format_date

5 common.inc format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL)
6 common.inc format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)
7 common.inc format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)
8 common.inc 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

▾ 41 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.
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
Returns the themed submitted-by string for the comment.
phptemplate_node_submitted in themes/garland/template.php
Returns the themed submitted-by string for the node.
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.

File

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

Code

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

Comments

$timezone argument important!

I used this function to convert a UNIX time stamp to a string in the format of YYYY-MM-DD:

$dateSQL = format_date($dateUnix, 'custom', 'Y-m-d');

This worked however if the user's timezone was different from the server's then it changed the actual date.
In my case the user's timezone was -05:00GMT and the server was -04:00GMT which resulted in the date 2010-03-31 changing to 2010-04-01.

The solution was to include the $timezone argument however since I did not want to change the timezone I passed an argument of 0 (zero).

$dateSQL = format_date($dateUnix, 'custom', 'Y-m-d', 0);

I also used the following code in another case:

$datePretty = format_date($dateUnix, 'medium', '', 0);

Note that I needed to pass an empty string ('') for the third argument (or I could have used null). The value of my second argument did not require the third argument ($format) to be set however without a third argument I could not pass the fourth ($format) and it is harmless to pass a value for $format as it will simply be ignored.

Problem with "r" custom format

I would suggest to change it to:

else if ($c == 'r') {
      $date .= format_date($timestamp - $timezone, 'custom', 'D, d M Y H:i:s O', $timezone, "en");
    }

RFC 2822 (http://www.faqs.org/rfcs/rfc2822.html) Section 3.3 doesn't allow the date-time format to be localized, so this creates problems on non-English Drupal sites when creating, for instance, RSS feeds.

The format_date() API

The format_date() API function documentation specifies that it supports the PHP date() function specifiers, but it does not actually support the "c", "e" and "P" specifiers.

Issue: http://drupal.org/node/494434

Login or register to post comments