You are here

private function SmartDateDurationFormatter::formatDurationTime in Smart Date 3.2.x

Same name and namespace in other branches
  1. 3.3.x src/Plugin/Field/FieldFormatter/SmartDateDurationFormatter.php \Drupal\smart_date\Plugin\Field\FieldFormatter\SmartDateDurationFormatter::formatDurationTime()
  2. 3.4.x src/Plugin/Field/FieldFormatter/SmartDateDurationFormatter.php \Drupal\smart_date\Plugin\Field\FieldFormatter\SmartDateDurationFormatter::formatDurationTime()

Format the string to be used as the datetime value.

Parameters

string $string: The string returned by DateFormatter::formatDiff.

Return value

string

1 call to SmartDateDurationFormatter::formatDurationTime()
SmartDateDurationFormatter::viewElements in src/Plugin/Field/FieldFormatter/SmartDateDurationFormatter.php

File

src/Plugin/Field/FieldFormatter/SmartDateDurationFormatter.php, line 198

Class

SmartDateDurationFormatter
Plugin implementation of a duration-based formatter for 'smartdate' fields.

Namespace

Drupal\smart_date\Plugin\Field\FieldFormatter

Code

private function formatDurationTime($string) {
  if (empty($string)) {
    return '';
  }
  $abbr_string = 'P';
  $intervals = [
    'Y' => 'year',
    'D' => 'day',
    'H' => 'hour',
    'M' => 'minute',
  ];
  foreach ($intervals as $key => $match_string) {
    $pattern = '/(\\d)+ ' . $match_string . '(s)?/i';
    preg_match($pattern, $string, $matches);
    if ($matches) {
      $abbr_string .= $matches[1] . $key;
    }
  }
  if (strlen($abbr_string) == 1) {
    $abbr_string = '';
  }
  return $abbr_string;
}