You are here

public function TimeSpan::setDuration in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/TypedData/Plugin/DataType/TimeSpan.php \Drupal\Core\TypedData\Plugin\DataType\TimeSpan::setDuration()

Sets the duration.

Parameters

\DateInterval $duration: A duration to set.

Overrides DurationInterface::setDuration

File

core/lib/Drupal/Core/TypedData/Plugin/DataType/TimeSpan.php, line 39

Class

TimeSpan
The time span data type represents durations as number of seconds.

Namespace

Drupal\Core\TypedData\Plugin\DataType

Code

public function setDuration(\DateInterval $duration, $notify = TRUE) {

  // Note that this applies the assumption of 12 month's a 30 days and
  // each year having 365 days. There is no accurate conversion for time spans
  // exceeding a day.
  $this->value = $duration->y * 365 * 24 * 60 * 60 + $duration->m * 30 * 24 * 60 * 60 + $duration->d * 24 * 60 * 60 + $duration->h * 60 * 60 + $duration->i * 60 + $duration->s;

  // Notify the parent of any changes.
  if ($notify && isset($this->parent)) {
    $this->parent
      ->onChange($this->name);
  }
}