8.5.x common.inc format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)
8.0.x common.inc format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)
8.1.x common.inc format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)
8.2.x common.inc format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)
8.3.x common.inc format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)
8.4.x common.inc format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)
8.6.x common.inc format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)
4.6.x common.inc format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL)
4.7.x common.inc format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL)
5.x common.inc format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL)
6.x common.inc format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)
7.x common.inc format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)

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


$timestamp: A UNIX timestamp to format.

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

  • 'short', 'medium', or 'long' (the corresponding built-in date formats).
  • 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

47 calls to format_date()
CommentPreviewTest::testCommentEditPreviewSave in modules/comment/comment.test
Test comment edit, preview, and save.
comment_admin_overview in modules/comment/comment.admin.inc
Form builder for the comment overview administration form.
comment_form in modules/comment/comment.module
Generate the basic commenting form, for appending to a node or display on a separate page.
DateTimeFunctionalTest::testDateFormatConfiguration in modules/system/system.test
Test date format configuration.
dblog_event in modules/dblog/dblog.admin.inc
Page callback: Displays details about a specific database log message.

... See full list


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


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

  // 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');
    case 'long':
      $format = variable_get('date_format_long', 'l, F j, Y - H:i');
    case 'custom':

      // No change to format.
    case 'medium':

      // Retrieve the format of the custom $type passed.
      if ($type != 'medium') {
        $format = variable_get('date_format_' . $type, '');

      // Fall back to 'medium'.
      if ($format === '') {
        $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(
  ), array(
  ), $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);


aiquandol’s picture

If you can't get this function to work, try using small, medium, and large for the date formats. I don't know if this is an error in documentation or because I'm running the date module.

masondib’s picture

It looks like the documentation on this page applies to the 7.x function which takes short, medium and long. The 6.x function takes small, medium and large, as aiquandol points out.

Maybe the 6 and 7 documentation should be separated?

paulsheldrake’s picture

Different languages need the date information ordered in different ways. If you pass the format string through t() then you can have translations done correctly

format_date(time(), 'custom', t('j M', array(), array('context' => 'php date format')));
tostinni’s picture

There's a simpler way to achieve this, in the Date and time menu there's a Localize option, go there (admin/config/regional/date-time/locale), edit your language and then you can define a new format for each of your types.

For example if you created a type month_day with a format M, jS (in english it would display as May, 5th), then in french set it to a new format (for example j M) and when you switch your interface to french it will show 5 Mai.

pandaPowder’s picture

@tostinni is right. here is a great explanation

oneiropolos’s picture

Yep but i don't want to switch the interface. I only want to translate the date format, not th whole site. Is there a way to achieve this? I've spent a lot of time searching

richard.c.allen2386’s picture

If you have a english time stamp format you can use strtotime with this as well to keep your dates formated.
<?php print format_date(strtotime($entity->field_date_of_birth['und'][0]['value2']) ,'custom','F j, Y'); ?>

redsd’s picture

Use $format_types = system_get_date_types();
to retrieve all available date formats.

Use $date_formats = system_get_date_formats();
To get the types with all the formats available.