You are here

public function DurationService::convertDateArrayToDurationString in Duration Field 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Service/DurationService.php \Drupal\duration_field\Service\DurationService::convertDateArrayToDurationString()

Convert array into an ISO 8601 duration string.

Parameters

array $input: An array containing the following keys:

  • y (year)
  • m (month)
  • d (day)
  • h (hour)
  • i (minute)
  • s (second)

Return value

string An ISO 8601 duration string.

Overrides DurationServiceInterface::convertDateArrayToDurationString

See also

http://en.wikipedia.org/wiki/Iso8601#Durations

2 calls to DurationService::convertDateArrayToDurationString()
DurationService::convertDateArrayToDateInterval in src/Service/DurationService.php
Convert a PHP DateInterval object to an ISO 8601 duration string.
DurationService::getDurationStringFromDateInterval in src/Service/DurationService.php
Converts a PHP DateINterval object to an ISO 8601 duration string.

File

src/Service/DurationService.php, line 32

Class

DurationService
Provides services for the Duration Field module.

Namespace

Drupal\duration_field\Service

Code

public function convertDateArrayToDurationString(array $input) {
  $duration = '';
  $date_mappings = [
    'y' => 'Y',
    'm' => 'M',
    'd' => 'D',
  ];
  foreach (array_keys($date_mappings) as $key) {
    if (isset($input[$key]) && $input[$key]) {
      $duration .= $input[$key] . $date_mappings[$key];
    }
  }
  $time_mappings = [
    'h' => 'H',
    'i' => 'M',
    's' => 'S',
  ];
  $found = FALSE;
  foreach (array_keys($time_mappings) as $key) {
    if (isset($input[$key]) && $input[$key]) {
      if (!$found) {
        $found = TRUE;
        $duration .= 'T';
      }
      $duration .= $input[$key] . $time_mappings[$key];
    }
  }
  return strlen($duration) ? 'P' . $duration : Iso8601StringInterface::EMPTY_DURATION;
}