Drupal 8  8.0.2
DateFormatter Class Reference
Inheritance diagram for DateFormatter:

Public Member Functions

 __construct (EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, TranslationInterface $translation, ConfigFactoryInterface $config_factory, RequestStack $request_stack)
 
 format ($timestamp, $type= 'medium', $format= '', $timezone=NULL, $langcode=NULL)
 
 formatInterval ($interval, $granularity=2, $langcode=NULL)
 
 getSampleDateFormats ($langcode=NULL, $timestamp=NULL, $timezone=NULL)
 
 formatTimeDiffUntil ($timestamp, $options=array())
 
 formatTimeDiffSince ($timestamp, $options=array())
 
 formatDiff ($from, $to, $options=array())
 

Protected Member Functions

 dateFormat ($format, $langcode)
 
 country ()
 

Protected Attributes

 $timezones
 
 $dateFormatStorage
 
 $languageManager
 
 $configFactory
 
 $requestStack
 
 $country = NULL
 
 $dateFormats = array()
 
 $units
 

Detailed Description

Provides a service to handle various date related functionality.

Constructor & Destructor Documentation

__construct ( EntityManagerInterface  $entity_manager,
LanguageManagerInterface  $language_manager,
TranslationInterface  $translation,
ConfigFactoryInterface  $config_factory,
RequestStack  $request_stack 
)

Constructs a Date object.

Parameters
\Drupal\Core\Entity\EntityManagerInterface$entity_managerThe entity manager.
\Drupal\Core\Language\LanguageManagerInterface$language_managerThe language manager.
\Drupal\Core\StringTranslation\TranslationInterface$translationThe string translation.
\Drupal\Core\Config\ConfigFactoryInterface$config_factoryThe configuration factory.
\Symfony\Component\HttpFoundation\RequestStack$request_stackThe request stack.

References Drupal\configFactory(), EntityTypeManagerInterface\getStorage(), Drupal\languageManager(), and Drupal\requestStack().

Here is the call graph for this function:

Member Function Documentation

country ( )
protected

Returns the default country from config.

Returns
string The config setting for country.default.

References Drupal\config().

Referenced by DateFormatter\format().

Here is the call graph for this function:

Here is the caller graph for this function:

dateFormat (   $format,
  $langcode 
)
protected

Loads the given format pattern for the given langcode.

Parameters
string$formatThe machine name of the date format.
string$langcodeThe langcode of the language to use.
Returns
string|null The pattern for the date format in the given language for non-custom formats, NULL otherwise.

References Drupal\languageManager().

Referenced by DateFormatter\format().

Here is the call graph for this function:

Here is the caller graph for this function:

format (   $timestamp,
  $type = 'medium',
  $format = '',
  $timezone = NULL,
  $langcode = NULL 
)

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

Parameters
int$timestampA UNIX timestamp to format.
string$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 date format config entity.
  • The machine name of an administrator-defined date format.
  • 'custom', to use $format. Defaults to 'medium'.
string$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.
string | null$timezone(optional) Time zone identifier, as described at http://php.net/manual/timezones.php Defaults to the time zone used to display the page.
string | null$langcode(optional) Language code to translate to. NULL (default) means to use the user interface language for the page.
Returns
string A translated date string in the requested format. Since the format may contain user input, this value should be escaped when output.
}

Implements DateFormatterInterface.

References DateFormatter\country(), DateTimePlus\createFromTimestamp(), DateFormatter\dateFormat(), and Drupal\languageManager().

Referenced by DateFormatter\getSampleDateFormats().

Here is the call graph for this function:

Here is the caller graph for this function:

formatDiff (   $from,
  $to,
  $options = array() 
)

{Formats a time interval between two timestamps.

Parameters
int$fromA UNIX timestamp, defining the from date and time.
int$toA UNIX timestamp, defining the to date and time.
array$options(optional) An associative array with additional options. The following keys can be used:
  • granularity: An integer value that signals how many different units to display in the string. Defaults to 2.
  • langcode: The language code for the language used to format the date. Defaults to NULL, which results in the user interface language for the page being used.
  • strict: A Boolean value indicating whether or not the $from timestamp can be after the $to timestamp. If TRUE (default) and $from is after $to, the result string will be "0 seconds". If FALSE and $from is after $to, the result string will be the formatted time difference.
Returns
string A translated string representation of the interval. This interval is always positive.
See Also
::formatInterval()
::formatTimeDiffSince()
::formatTimeDiffUntil()
}

Implements DateFormatterInterface.

References t().

Referenced by DateFormatter\formatTimeDiffSince(), and DateFormatter\formatTimeDiffUntil().

Here is the call graph for this function:

Here is the caller graph for this function:

formatInterval (   $interval,
  $granularity = 2,
  $langcode = NULL 
)

{Formats a time interval with the requested granularity.Note that for intervals over 30 days, the output is approximate: a "month" is always exactly 30 days, and a "year" is always 365 days. It is not possible to make a more exact representation, given that there is only one input in seconds. If you are formatting an interval between two specific timestamps, use ::formatDiff() instead.

Parameters
int$intervalThe length of the interval in seconds.
int$granularity(optional) How many different units to display in the string (2 by default).
string | null$langcode(optional) langcode: The language code for the language used to format the date. Defaults to NULL, which results in the user interface language for the page being used.
Returns
string A translated string representation of the interval.
See Also
::formatDiff()
}

Implements DateFormatterInterface.

References t().

Here is the call graph for this function:

formatTimeDiffSince (   $timestamp,
  $options = array() 
)

{Formats the time difference from a timestamp to the current request time.

Parameters
$timestampA UNIX timestamp to compare against the current request time.
array$options(optional) An associative array with additional options. The following keys can be used:
  • granularity: An integer value that signals how many different units to display in the string. Defaults to 2.
  • langcode: The language code for the language used to format the date. Defaults to NULL, which results in the user interface language for the page being used.
  • strict: A Boolean value indicating whether or not the timestamp can be after the current request time. If TRUE (default) and $timestamp is after the current request time, the result string will be "0 seconds". If FALSE and $timestamp is after the current request time, the result string will be the formatted time difference.
Returns
string A translated string representation of the difference between the given timestamp and the current request time. This interval is always positive.
See Also
::formatDiff()
::formatTimeDiffUntil()
}

Implements DateFormatterInterface.

References DateFormatter\formatDiff(), and Drupal\requestStack().

Here is the call graph for this function:

formatTimeDiffUntil (   $timestamp,
  $options = array() 
)

{Formats the time difference from the current request time to a timestamp.

Parameters
$timestampA UNIX timestamp to compare against the current request time.
array$options(optional) An associative array with additional options. The following keys can be used:
  • granularity: An integer value that signals how many different units to display in the string. Defaults to 2.
  • langcode: The language code for the language used to format the date. Defaults to NULL, which results in the user interface language for the page being used.
  • strict: A Boolean value indicating whether or not the timestamp can be before the current request time. If TRUE (default) and $timestamp is before the current request time, the result string will be "0 seconds". If FALSE and $timestamp is before the current request time, the result string will be the formatted time difference.
Returns
string A translated string representation of the difference between the given timestamp and the current request time. This interval is always positive.
See Also
::formatDiff()
::formatTimeDiffSince()
}

Implements DateFormatterInterface.

References DateFormatter\formatDiff(), and Drupal\requestStack().

Here is the call graph for this function:

getSampleDateFormats (   $langcode = NULL,
  $timestamp = NULL,
  $timezone = NULL 
)

{Provides values for all date formatting characters for a given timestamp.

Parameters
string | null$langcode(optional) Language code of the date format, if different from the site default language.
int | null$timestamp(optional) The Unix timestamp to format, defaults to current time.
string | null$timezone(optional) The timezone to use, if different from the site's default timezone.
Returns
array An array of formatted date values, indexed by the date format character.
See Also
date()
}

Implements DateFormatterInterface.

References DateFormatter\format().

Here is the call graph for this function:

Field Documentation

$units
protected
Initial value:
= array(
'1 year|@count years' => 31536000,
'1 month|@count months' => 2592000,
'1 week|@count weeks' => 604800,
'1 day|@count days' => 86400,
'1 hour|@count hours' => 3600,
'1 min|@count min' => 60,
'1 sec|@count sec' => 1,
)

The documentation for this class was generated from the following file: