Formatting


Functions

 format_rss_channel ($title, $link, $description, $items, $langcode=NULL, $args=array())
 format_rss_item ($title, $link, $description, $args=array())
 format_xml_elements ($array)
 format_plural ($count, $singular, $plural, $args=array(), $langcode=NULL)
 parse_size ($size)
 format_size ($size, $langcode=NULL)
 format_interval ($timestamp, $granularity=2, $langcode=NULL)
 format_date ($timestamp, $type= 'medium', $format= '', $timezone=NULL, $langcode=NULL)

Detailed Description

Functions to format numbers, strings, dates, etc.

Function Documentation

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.
Returns:
A translated date string in the requested format.

format_interval ( timestamp,
granularity = 2,
langcode = NULL 
)

Format a time interval with the requested granularity.

Parameters:
$timestamp The length of the interval in seconds.
$granularity How many different units to display in the string.
$langcode Optional language code to translate to a language other than what is used to display the page.
Returns:
A translated string representation of the interval.

format_plural ( count,
singular,
plural,
args = array(),
langcode = NULL 
)

Format a string containing a count of items.

This function ensures that the string is pluralized correctly. Since t() is called by this function, make sure not to pass already-localized strings to it.

For example:

   $output = format_plural($node->comment_count, '1 comment', '@count comments');

Example with additional replacements:

   $output = format_plural($update_count,
     'Changed the content type of 1 post from %old-type to %new-type.',
     'Changed the content type of @count posts from %old-type to %new-type.',
     array('%old-type' => $info->old_type, '%new-type' => $info->new_type)));

Parameters:
$count The item count to display.
$singular The string for the singular case. Please make sure it is clear this is singular, to ease translation (e.g. use "1 new comment" instead of "1 new"). Do not use in the singular string.
$plural The string for the plural case. Please make sure it is clear this is plural, to ease translation. Use in place of the item count, as in " new comments".
$args An associative array of replacements to make after translation. Incidences of any key in this array are replaced with the corresponding value. Based on the first character of the key, the value is escaped and/or themed:
  • !variable: inserted as is
  • : escape plain text to HTML (check_plain)
  • variable: escape text and theme as a placeholder for user-submitted content (check_plain + theme_placeholder) Note that you do not need to include in this array. This replacement is done automatically for the plural case.
$langcode Optional language code to translate to a language other than what is used to display the page.
Returns:
A translated string.

format_rss_channel ( title,
link,
description,
items,
langcode = NULL,
args = array() 
)

Formats an RSS channel.

Arbitrary elements may be added using the $args associative array.

format_rss_item ( title,
link,
description,
args = array() 
)

Format a single RSS item.

Arbitrary elements may be added using the $args associative array.

format_size ( size,
langcode = NULL 
)

Generate a string representation for the given byte count.

Parameters:
$size A size in bytes.
$langcode Optional language code to translate to a language other than what is used to display the page.
Returns:
A translated string representation of the size.

format_xml_elements ( array  ) 

Format XML elements.

Parameters:
$array An array where each item represent an element and is either a:
  • (key => value) pair (<key>value</key>)
  • Associative array with fields:
    • 'key': element name
    • 'value': element contents
    • 'attributes': associative array of element attributes
In both cases, 'value' can be a simple string, or it can be another array with the same format as $array itself for nesting.

parse_size ( size  ) 

Parse a given byte count.

Parameters:
$size A size expressed as a number of bytes with optional SI size and unit suffix (e.g. 2, 3K, 5MB, 10G).
Returns:
An integer representation of the size.