Transforms a date interval into an array of date interval elements.

@param DateInterval $dateInterval Date interval.

@return array Date interval elements.

@throws TransformationFailedException If the given value is not an instance of DateInterval
174847
public function transform($dateInterval)
    {
        if ($dateInterval === null) {
            return [
                'year' => '0',
                'month' => '0',
                'day' => '0',
                'hour' => '0',
                'minute' => '0',
                'second' => '0',
            ];
        }

        if (!$dateInterval instanceof DateInterval) {
            throw new TransformationFailedException('Expected a DateInterval.');
        }

        $result = [
            'year' => $dateInterval->format('%y'),
            'month' => $dateInterval->format('%m'),
            'day' => $dateInterval->format('%d'),
            'hour' => $dateInterval->format('%h'),
            'minute' => $dateInterval->format('%i'),
            'second' => $dateInterval->format('%s'),
        ];

        return $result;
    }
199672
public function setDateInterval($interval)
    {
        if (!$interval = CarbonInterval::make($interval)) {
            throw new InvalidArgumentException('Invalid interval.');
        }

        if ($interval->spec() === 'PT0S') {
            throw new InvalidArgumentException('Empty interval is not accepted.');
        }

        $this->dateInterval = $interval;

        $this->isDefaultInterval = false;

        $this->handleChangedParameters();

        return $this;
    }
633824
public function each(\DateInterval $interval, \Closure $callback)
    {
        $period = new static($this->start(), $this->start()->add($interval));

        do {
            $callback(new static(
                $period->start(),
                $period->end() > $this->endDate ? $this->endDate : $period->end()
            ));
        } while ($period->add($interval)->start() < $this->endDate);

        return $this;
    }
843184
public function add(TimeInterval $interval)
    {
        $date = clone $this->date;
        $int = new DateInterval($interval->intervalSpec());
        $date->add($int);
        return $this->dateTimeToTimePoint($date);
    }
955398
public function add($interval = null)
	{
		if ( ! $interval instanceOf DateInterval)
		{
			if (is_numeric($interval))
			{
				$delta = intval($interval);
				$interval = new DateInterval('PT'.strval(abs($delta)).'S');
				if ($delta < 0)
				{
					$interval->invert = 1;
				}
			}
		}

		if ($this->datetime->add($interval))
		{
			return $this;
		}

		return false;
	}
27747
public function formatInterval(\DateInterval $dateInterval): string
    {
        $format = 'P';

        if (0 < $dateInterval->y) {
            $format .= $dateInterval->y.'Y';
        }

        if (0 < $dateInterval->m) {
            $format .= $dateInterval->m.'M';
        }

        if (0 < $dateInterval->d) {
            $format .= $dateInterval->d.'D';
        }

        if (0 < $dateInterval->h || 0 < $dateInterval->i || 0 < $dateInterval->s) {
            $format .= 'T';
        }

        if (0 < $dateInterval->h) {
            $format .= $dateInterval->h.'H';
        }

        if (0 < $dateInterval->i) {
            $format .= $dateInterval->i.'M';
        }

        if (0 < $dateInterval->s) {
            $format .= $dateInterval->s.'S';
        }

        return $format;
    }
703747
public static function instance(DateInterval $di)
    {
        $microseconds = $di->f;
        $instance = new static(static::getDateIntervalSpec($di));
        if ($microseconds) {
            $instance->f = $microseconds;
        }
        $instance->invert = $di->invert;
        foreach (['y', 'm', 'd', 'h', 'i', 's'] as $unit) {
            if ($di->$unit < 0) {
                $instance->$unit *= -1;
            }
        }

        return $instance;
    }
11686
public function add(/*\DateInterval*/ $interval) : \DateTime /*self invariant*/
    {
        // NB: Argument type hinting (\DateInterval $interval)
        // would provoke E_WARNING when cloning.
        // Catch 22: Specs say that native \DateTime method is type hinted,
        // but warning when cloning says it isn't.

        if ($this->frozen) {
            throw new \RuntimeException(get_class($this) . ' is read-only, frozen.');
        }
        return parent::add($interval);
    }
492953
public function equals(DateInterval $interval)
	{
		$years   = $this->y == $interval->y;
		$months  = $this->m == $interval->m;
		$days    = $this->d == $interval->d;
		$hours   = $this->h == $interval->h;
		$minutes = $this->i == $interval->i;
		$seconds = $this->s == $interval->s;
		$invert  = $this->invert == $interval->invert;

		return $years && $months && $days && $hours && $minutes && $seconds && $invert;
	}
1046857
public static function generate(\DateTimeInterface $date): array
    {
        $list = array();
        $list[] = $date->format('ym');
        $list[] = $date->format('yd');
        $list[] = $date->format('my');
        $list[] = $date->format('md');
        $list[] = $date->format('dy');
        $list[] = $date->format('dm');

        return array_unique($list);
    }
809359
private function convertDateParams($dateFrom, $dateTo)
    {
        $dateFromConverted = new \DateTime($dateFrom);

        $dateToConverted = new \DateTime($dateTo);
        $dateToConverted->modify('+1 day -1 second');

        return [
            $dateFromConverted->getTimestamp(),
            $dateToConverted->getTimestamp(),
        ];
    }
1163434
public function transform($dateTime)
    {
        if (null === $dateTime) {
            return '';
        }

        if (!$dateTime instanceof \DateTime) {
            throw new TransformationFailedException('Expected a \DateTime.');
        }

        // convert time to UTC before passing it to the formatter
        $dateTime = clone $dateTime;
        $value = $this->getIntlDateFormatter()->format((int) $dateTime->format('U'));

        return $value;
    }
1319212
public static function instance(DateInterval $di)
    {
        $instance = new static(static::getDateIntervalSpec($di));
        $instance->invert = $di->invert;

        return $instance;
    }
1246472
private function dateIntervalToIntervalSpec(DateInterval $interval)
    {
        $date = sprintf('%uY%uM%uD', $interval->y, $interval->m, $interval->d);
        $time = sprintf('%uH%uM%uS', $interval->h, $interval->i, $interval->s);

        // build extra spec
        $extra = '';
        if ($interval->invert) {
            $extra .= 'I';
        }

        if ($interval->days) {
            $extra .= $interval->days . 'D';
        }

        if (strlen($extra) > 0) {
            $extra = '-' . $extra;
        }

        return sprintf('P%sT%s%s', $date, $time, $extra);
    }
340516
public static function serialize(Serializer $serializer, DateInterval $dateInterval)
    {
        return array(
            Serializer::CLASS_IDENTIFIER_KEY => \get_class($dateInterval),
            'construct' => array(
                Serializer::SCALAR_TYPE => 'string',
                Serializer::SCALAR_VALUE => \sprintf(
                    'P%sY%sM%sDT%sH%sM%sS',
                    $dateInterval->y,
                    $dateInterval->m,
                    $dateInterval->d,
                    $dateInterval->h,
                    $dateInterval->i,
                    $dateInterval->s
                ),
            ),
            'invert' => array(
                Serializer::SCALAR_TYPE => 'integer',
                Serializer::SCALAR_VALUE => (empty($dateInterval->invert)) ? 0 : 1,
            ),
            'days' => array(
                Serializer::SCALAR_TYPE => \gettype($dateInterval->days),
                Serializer::SCALAR_VALUE => $dateInterval->days,
            ),
        );
    }
20306
public function transform($value)
    {
        if (null !== $value && !is_scalar($value)) {
            throw new TransformationFailedException('Expected a scalar.');
        }

        if ('' === $value || null === $value) {
            return null;
        }

        return [$value];
    }
644522
public function toSeconds(\DateInterval $interval = null): string
    {
        if ($interval === null) {
            $interval = $this;
        }

        return static::intervalToSeconds($interval);
    }