function format_date

Formats a date, using a date type or a custom date format string.

Parameters

$timestamp: A UNIX timestamp to format.

$type: (optional) The format to use, one of:

  • One of the built-in formats: 'short', 'medium', 'long', 'html_datetime', 'html_date', 'html_time', 'html_yearless_date', 'html_week', 'html_month', 'html_year'.
  • The name of a date type defined by a module in hook_date_format_types(), if it's been assigned a format.
  • The machine name of an administrator-defined date format.
  • 'custom', to use $format.

Defaults to 'medium'.

$format: (optional) If $type is 'custom', a PHP date format string suitable for input to date(). Use a backslash to escape ordinary text, so it does not get interpreted as date format characters.

$timezone: (optional) Time zone identifier, as described at http://php.net/manual/timezones.php Defaults to the time zone used to display the page.

$langcode: (optional) Language code to translate to. Defaults to the language used to display the page.

Return value

A translated date string in the requested format.

Related topics

65 calls to format_date()
CommentFormController::form in drupal/core/modules/comment/lib/Drupal/comment/CommentFormController.php
Overrides Drupal\Core\Entity\EntityFormController::form().
CommentPreviewTest::testCommentEditPreviewSave in drupal/core/modules/comment/lib/Drupal/comment/Tests/CommentPreviewTest.php
Tests comment edit, preview, and save.
comment_admin_overview in drupal/core/modules/comment/comment.admin.inc
Form constructor for the comment overview administration form.
comment_tokens in drupal/core/modules/comment/comment.tokens.inc
Implements hook_tokens().
CreatedDay::summary_name in drupal/core/modules/node/lib/Drupal/node/Plugin/views/argument/CreatedDay.php
Provide a link to the next level of the view

... See full list

File

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

Code

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_fast;
  if (!isset($drupal_static_fast)) {
    $drupal_static_fast['timezones'] =& drupal_static(__FUNCTION__);
  }
  $timezones =& $drupal_static_fast['timezones'];
  if (!isset($timezone)) {
    $timezone = date_default_timezone_get();
  }

  // Store DateTimeZone objects in an array rather than repeatedly
  // constructing identical objects over the life of a request.
  if (!isset($timezones[$timezone])) {
    $timezones[$timezone] = timezone_open($timezone);
  }
  if (empty($langcode)) {
    $langcode = language(LANGUAGE_TYPE_INTERFACE)->langcode;
  }

  // Create a DrupalDateTime object from the timestamp and timezone.
  $date = new DrupalDateTime($timestamp, $timezones[$timezone]);

  // Find the appropriate format type.
  $key = $date
    ->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP;

  // If we have a non-custom date format use the provided date format pattern.
  if ($type != 'custom') {
    $format = config('system.date')
      ->get('formats.' . $type . '.pattern.' . $key);
  }

  // Fall back to medium if a format was not found.
  if (empty($format)) {
    $format = config('system.date')
      ->get('formats.medium.pattern.' . $key);
  }

  // Call $date->format().
  $settings = array(
    'langcode' => $langcode,
    'format_string_type' => $key,
  );
  return $date
    ->format($format, $settings);
}