function DateFormatter::format
Same name in other branches
- 8.9.x core/lib/Drupal/Core/Datetime/DateFormatter.php \Drupal\Core\Datetime\DateFormatter::format()
- 10 core/lib/Drupal/Core/Datetime/DateFormatter.php \Drupal\Core\Datetime\DateFormatter::format()
- 11.x core/lib/Drupal/Core/Datetime/DateFormatter.php \Drupal\Core\Datetime\DateFormatter::format()
Overrides DateFormatterInterface::format
1 call to DateFormatter::format()
- DateFormatter::getSampleDateFormats in core/
lib/ Drupal/ Core/ Datetime/ DateFormatter.php - Provides values for all date formatting characters for a given timestamp.
File
-
core/
lib/ Drupal/ Core/ Datetime/ DateFormatter.php, line 103
Class
- DateFormatter
- Provides a service to handle various date related functionality.
Namespace
Drupal\Core\DatetimeCode
public function format($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
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($this->timezones[$timezone])) {
$this->timezones[$timezone] = timezone_open($timezone);
}
if (empty($langcode)) {
$langcode = $this->languageManager
->getCurrentLanguage()
->getId();
}
// Create a DrupalDateTime object from the timestamp and timezone.
$create_settings = [
'langcode' => $langcode,
'country' => $this->country(),
];
$date = DrupalDateTime::createFromTimestamp($timestamp, $this->timezones[$timezone], $create_settings);
// If we have a non-custom date format use the provided date format pattern.
if ($type !== 'custom') {
if ($date_format = $this->dateFormat($type, $langcode)) {
$format = $date_format->getPattern();
}
}
// Fall back to the 'medium' date format type if the format string is
// empty, either from not finding a requested date format or being given an
// empty custom format string.
if (empty($format)) {
$format = $this->dateFormat('fallback', $langcode)
->getPattern();
}
// Call $date->format().
$settings = [
'langcode' => $langcode,
];
return $date->format($format, $settings);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.