DateFormatter
class DateFormatter implements DateFormatterInterface (View source)
Provides a service to handle various date related functionality.
Traits
Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.
Properties
| protected TranslationInterface | $stringTranslation | The string translation service. |
from StringTranslationTrait |
| protected array | $timezones | The list of loaded timezones. |
|
| protected EntityStorageInterface | $dateFormatStorage | The date format storage. |
|
| protected LanguageManagerInterface | $languageManager | Language manager for retrieving the default langcode when none is specified. |
|
| protected ConfigFactoryInterface | $configFactory | The configuration factory. |
|
| protected RequestStack | $requestStack | The request stack. |
|
| protected | $country | ||
| protected | $dateFormats | ||
| protected array | $units | Contains the different date interval units. |
Methods
Translates a string to the current language or to a given language.
Formats a string containing a count of items.
Returns the number of plurals supported by a given language.
Gets the string translation service.
Constructs a Date object.
Formats a date, using a date type or a custom date format string.
Formats a time interval with the requested granularity.
Provides values for all date formatting characters for a given timestamp.
Formats the time difference from the current request time to a timestamp.
Formats the time difference from a timestamp to the current request time.
Formats a time interval between two timestamps.
Loads the given format pattern for the given langcode.
Returns the default country from config.
Details
protected TranslatableMarkup
t(string $string, array $args = [], array $options = [])
Translates a string to the current language or to a given language.
See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.
In order for strings to be localized, make them available in one of the ways supported by the
protected
formatPlural($count, $singular, $plural, array $args = [], array $options = [])
Formats a string containing a count of items.
protected
getNumberOfPlurals($langcode = NULL)
Returns the number of plurals supported by a given language.
protected TranslationInterface
getStringTranslation()
Gets the string translation service.
$this
setStringTranslation(TranslationInterface $translation)
Sets the string translation service to use.
__construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, TranslationInterface $translation, ConfigFactoryInterface $config_factory, RequestStack $request_stack)
Constructs a Date object.
string
format(int $timestamp, string $type = 'medium', string $format = '', string|null $timezone = NULL, string|null $langcode = NULL)
Formats a date, using a date type or a custom date format string.
string
formatInterval(int $interval, int $granularity = 2, string|null $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 \Drupal\Core\Datetime\DateFormatter::formatDiff() instead.
array
getSampleDateFormats(string|null $langcode = NULL, int|null $timestamp = NULL, string|null $timezone = NULL)
Provides values for all date formatting characters for a given timestamp.
string|FormattedDateDiff
formatTimeDiffUntil($timestamp, array $options = [])
Formats the time difference from the current request time to a timestamp.
string|FormattedDateDiff
formatTimeDiffSince($timestamp, array $options = [])
Formats the time difference from a timestamp to the current request time.
string|FormattedDateDiff
formatDiff(int $from, int $to, array $options = [])
Formats a time interval between two timestamps.
protected DateFormatInterface|null
dateFormat(string $format, string $langcode)
Loads the given format pattern for the given langcode.
protected string
country()
Returns the default country from config.