<?php
 namespace Eluceo\iCal\Presentation\Component\Property\Value; use DateInterval; use DateTimeImmutable; use Eluceo\iCal\Presentation\Component\Property\Value; final class DurationValue extends Value { private DateInterval $duration; public function __construct(DateInterval $duration) { $this->duration = $duration; } public function __toString(): string { $duration = $this->getNormalizedDateInterval(); $durationAsString = $duration->invert === 1 ? '-P' : 'P'; $days = abs($duration->days); if ($days > 0) { $durationAsString .= $days . 'D'; } $hours = abs($duration->h); $minutes = abs($duration->i); $seconds = abs($duration->s); if ($hours > 0 || $minutes > 0 || $seconds > 0) { $durationAsString .= 'T'; if ($hours > 0) { $durationAsString .= $hours . 'H'; } if ($minutes > 0) { $durationAsString .= $minutes . 'M'; } if ($seconds > 0) { $durationAsString .= $seconds . 'S'; } } return $durationAsString; } private function getNormalizedDateInterval(): DateInterval { $baseDate = (new DateTimeImmutable())->setTimestamp(0); $nextDate = $baseDate->sub($this->duration); return $nextDate->diff($baseDate); } } 