You are here

protected function DateTimeTimeAgoFormatter::formatDate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeTimeAgoFormatter.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeTimeAgoFormatter::formatDate()

Formats a date/time as a time interval.

Parameters

\Drupal\Core\Datetime\DrupalDateTime|object $date: A date/time object.

Return value

string The formatted date/time string using the past or future format setting.

2 calls to DateTimeTimeAgoFormatter::formatDate()
DateTimeTimeAgoFormatter::settingsSummary in core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeTimeAgoFormatter.php
Returns a short summary for the current formatter settings.
DateTimeTimeAgoFormatter::viewElements in core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeTimeAgoFormatter.php
Builds a renderable array for a field value.

File

core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeTimeAgoFormatter.php, line 182
Contains \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeTimeAgoFormatter.

Class

DateTimeTimeAgoFormatter
Plugin implementation of the 'Time ago' formatter for 'datetime' fields.

Namespace

Drupal\datetime\Plugin\Field\FieldFormatter

Code

protected function formatDate(DrupalDateTime $date) {
  $granularity = $this
    ->getSetting('granularity');
  $timestamp = $date
    ->getTimestamp();
  $options = [
    'granularity' => $granularity,
  ];
  if ($this->request->server
    ->get('REQUEST_TIME') > $timestamp) {
    return SafeMarkup::format($this
      ->getSetting('past_format'), [
      '@interval' => $this->dateFormatter
        ->formatTimeDiffSince($timestamp, $options),
    ]);
  }
  else {
    return SafeMarkup::format($this
      ->getSetting('future_format'), [
      '@interval' => $this->dateFormatter
        ->formatTimeDiffUntil($timestamp, $options),
    ]);
  }
}