View source
<?php
namespace Drupal\Core\Datetime;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\HttpFoundation\RequestStack;
class DateFormatter implements DateFormatterInterface {
use StringTranslationTrait;
protected $timezones;
protected $dateFormatStorage;
protected $languageManager;
protected $configFactory;
protected $requestStack;
protected $country = NULL;
protected $dateFormats = [];
protected $units = [
'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,
];
public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, TranslationInterface $translation, ConfigFactoryInterface $config_factory, RequestStack $request_stack) {
$this->dateFormatStorage = $entity_type_manager
->getStorage('date_format');
$this->languageManager = $language_manager;
$this->stringTranslation = $translation;
$this->configFactory = $config_factory;
$this->requestStack = $request_stack;
}
public function format($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
if (!isset($timezone)) {
$timezone = date_default_timezone_get();
}
if (!isset($this->timezones[$timezone])) {
$this->timezones[$timezone] = timezone_open($timezone);
}
if (empty($langcode)) {
$langcode = $this->languageManager
->getCurrentLanguage()
->getId();
}
$create_settings = [
'langcode' => $langcode,
'country' => $this
->country(),
];
$date = DrupalDateTime::createFromTimestamp($timestamp, $this->timezones[$timezone], $create_settings);
if ($type !== 'custom') {
if ($date_format = $this
->dateFormat($type, $langcode)) {
$format = $date_format
->getPattern();
}
}
if (empty($format)) {
$format = $this
->dateFormat('fallback', $langcode)
->getPattern();
}
$settings = [
'langcode' => $langcode,
];
return $date
->format($format, $settings);
}
public function formatInterval($interval, $granularity = 2, $langcode = NULL) {
$output = '';
foreach ($this->units as $key => $value) {
$key = explode('|', $key);
if ($interval >= $value) {
$output .= ($output ? ' ' : '') . $this
->formatPlural(floor($interval / $value), $key[0], $key[1], [], [
'langcode' => $langcode,
]);
$interval %= $value;
$granularity--;
}
elseif ($output) {
break;
}
if ($granularity == 0) {
break;
}
}
return $output ? $output : $this
->t('0 sec', [], [
'langcode' => $langcode,
]);
}
public function getSampleDateFormats($langcode = NULL, $timestamp = NULL, $timezone = NULL) {
$timestamp = $timestamp ?: time();
$date_chars = str_split('dDjlNSwzWFmMntLoYyaABgGhHisueIOPTZcrU');
$date_elements = array_combine($date_chars, $date_chars);
return array_map(function ($character) use ($timestamp, $timezone, $langcode) {
return $this
->format($timestamp, 'custom', $character, $timezone, $langcode);
}, $date_elements);
}
public function formatTimeDiffUntil($timestamp, $options = []) {
$request_time = $this->requestStack
->getCurrentRequest()->server
->get('REQUEST_TIME');
return $this
->formatDiff($request_time, $timestamp, $options);
}
public function formatTimeDiffSince($timestamp, $options = []) {
$request_time = $this->requestStack
->getCurrentRequest()->server
->get('REQUEST_TIME');
return $this
->formatDiff($timestamp, $request_time, $options);
}
public function formatDiff($from, $to, $options = []) {
$options += [
'granularity' => 2,
'langcode' => NULL,
'strict' => TRUE,
'return_as_object' => FALSE,
];
if ($options['strict'] && $from > $to) {
$string = $this
->t('0 seconds');
if ($options['return_as_object']) {
return new FormattedDateDiff($string, 0);
}
return $string;
}
$date_time_from = new \DateTime();
$date_time_from
->setTimestamp($from);
$date_time_to = new \DateTime();
$date_time_to
->setTimestamp($to);
$interval = $date_time_to
->diff($date_time_from);
$granularity = $options['granularity'];
$output = '';
$max_age = 1.0E+99;
foreach ([
'y',
'm',
'd',
'h',
'i',
's',
] as $value) {
if ($interval->{$value} > 0) {
switch ($value) {
case 'y':
$interval_output = $this
->formatPlural($interval->y, '1 year', '@count years', [], [
'langcode' => $options['langcode'],
]);
$max_age = min($max_age, 365 * 86400);
break;
case 'm':
$interval_output = $this
->formatPlural($interval->m, '1 month', '@count months', [], [
'langcode' => $options['langcode'],
]);
$max_age = min($max_age, 30 * 86400);
break;
case 'd':
$interval_output = '';
$days = $interval->d;
$weeks = floor($days / 7);
if ($weeks) {
$interval_output .= $this
->formatPlural($weeks, '1 week', '@count weeks', [], [
'langcode' => $options['langcode'],
]);
$days -= $weeks * 7;
$granularity--;
$max_age = min($max_age, 7 * 86400);
}
if ((!$output || $weeks > 0) && $granularity > 0 && $days > 0) {
$interval_output .= ($interval_output ? ' ' : '') . $this
->formatPlural($days, '1 day', '@count days', [], [
'langcode' => $options['langcode'],
]);
$max_age = min($max_age, 86400);
}
else {
$granularity = 0;
}
break;
case 'h':
$interval_output = $this
->formatPlural($interval->h, '1 hour', '@count hours', [], [
'langcode' => $options['langcode'],
]);
$max_age = min($max_age, 3600);
break;
case 'i':
$interval_output = $this
->formatPlural($interval->i, '1 minute', '@count minutes', [], [
'langcode' => $options['langcode'],
]);
$max_age = min($max_age, 60);
break;
case 's':
$interval_output = $this
->formatPlural($interval->s, '1 second', '@count seconds', [], [
'langcode' => $options['langcode'],
]);
$max_age = min($max_age, 1);
break;
}
$output .= ($output && $interval_output ? ' ' : '') . $interval_output;
$granularity--;
}
elseif ($output) {
break;
}
if ($granularity <= 0) {
break;
}
}
if (empty($output)) {
$output = $this
->t('0 seconds');
$max_age = 0;
}
if ($options['return_as_object']) {
return new FormattedDateDiff($output, $max_age);
}
return $output;
}
protected function dateFormat($format, $langcode) {
if (!isset($this->dateFormats[$format][$langcode])) {
$original_language = $this->languageManager
->getConfigOverrideLanguage();
$this->languageManager
->setConfigOverrideLanguage(new Language([
'id' => $langcode,
]));
$this->dateFormats[$format][$langcode] = $this->dateFormatStorage
->load($format);
$this->languageManager
->setConfigOverrideLanguage($original_language);
}
return $this->dateFormats[$format][$langcode];
}
protected function country() {
if ($this->country === NULL) {
$this->country = \Drupal::config('system.date')
->get('country.default');
}
return $this->country;
}
}