function TimestampAgoFormatter::formatTimestamp

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampAgoFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampAgoFormatter::formatTimestamp()
  2. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampAgoFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampAgoFormatter::formatTimestamp()
  3. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampAgoFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampAgoFormatter::formatTimestamp()

Formats a timestamp.

Parameters

int $timestamp: A UNIX timestamp to format.

Return value

array The formatted timestamp string using the past or future format setting.

2 calls to TimestampAgoFormatter::formatTimestamp()
DateTimeTimeAgoFormatter::formatDate in core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeTimeAgoFormatter.php
Formats a date/time as a time interval.
TimestampAgoFormatter::viewElements in core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampAgoFormatter.php
Builds a renderable array for a field value.

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampAgoFormatter.php, line 192

Class

TimestampAgoFormatter
Plugin implementation of the 'timestamp' formatter as time ago.

Namespace

Drupal\Core\Field\Plugin\Field\FieldFormatter

Code

protected function formatTimestamp($timestamp) {
  $granularity = $this->getSetting('granularity');
  $options = [
    'granularity' => $granularity,
    'return_as_object' => TRUE,
  ];
  if ($this->request->server
    ->get('REQUEST_TIME') > $timestamp) {
    $result = $this->dateFormatter
      ->formatTimeDiffSince($timestamp, $options);
    $build = [
      '#markup' => new FormattableMarkup($this->getSetting('past_format'), [
        '@interval' => $result->getString(),
      ]),
    ];
  }
  else {
    $result = $this->dateFormatter
      ->formatTimeDiffUntil($timestamp, $options);
    $build = [
      '#markup' => new FormattableMarkup($this->getSetting('future_format'), [
        '@interval' => $result->getString(),
      ]),
    ];
  }
  CacheableMetadata::createFromObject($result)->applyTo($build);
  return $build;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.