Add an interval to a date and return a new DateTime object

@param \DateTime $date
@param string|\DateInterval $interval
@return \DateTime
776947
public function add($date, $interval)
    {
        if (!$interval instanceof \DateInterval) {
            $interval = new \DateInterval($interval);
        }
        $result = clone $date;
        return $result->add($interval);
    }
212594
public static function modify($dateAsString, $interval, $format = self::DEFAULT_TIME_FORMAT)
    {
        $dateTime = new DateTime($dateAsString);
        return $dateTime->modify($interval)->format($format);
    }
1160610
protected function _getPeriodByDate(DateTime $Date, $interval = null) {
		$Range = new DateRange(clone $Date);
		return $Range->period($this->_getInterval($interval));
	}
302984
public function timeAgo($date)
    {
        $interval = $this->getInterval(time(), $date);
        if ($interval->invert) {
            return $this->convert(time(), $date);
        }

        return $this->translation->translate();
    }
1050846
public Interval withEndDate(LocalDate date) {
        requireNonNull(date);
        return new Interval(startDate, startTime, date, endTime, zoneId);
    }
1319212
public static function instance(DateInterval $di)
    {
        $instance = new static(static::getDateIntervalSpec($di));
        $instance->invert = $di->invert;

        return $instance;
    }
870406
public function applyDateInterval($interval, $unit)
    {
        if ($interval > 0) {
            $this->add(new RhubarbDateInterval("P{$interval}{$unit}"));
        } elseif ($interval < 0) {
            $interval *= -1;
            $this->sub(new RhubarbDateInterval("P{$interval}{$unit}"));
        }
    }
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);
    }
429845
private static function dateIntervalToString(\DateInterval $interval)
    {

        // Reading all non-zero date parts.
        $date = array_filter([
            'Y' => $interval->y,
            'M' => $interval->m,
            'D' => $interval->d,
        ]);

        // Reading all non-zero time parts.
        $time = array_filter([
            'H' => $interval->h,
            'M' => $interval->i,
            'S' => $interval->s,
        ]);

        $specString = 'P';

        // Adding each part to the spec-string.
        foreach ($date as $key => $value) {
            $specString .= $value.$key;
        }
        if (count($time) > 0) {
            $specString .= 'T';
            foreach ($time as $key => $value) {
                $specString .= $value.$key;
            }
        }

        return $specString;
    }
708980
public function differenceDateTime($dateTime = null) {
        if (!empty($dateTime)) {
            $this->setDateTime($dateTime, true);
        }
        $this->dateTimeInterval = $interval = $this->dateTime->diff($this->dateTimeDiff);
        return $interval;
    }
835047
public Interval withStartDateTime(LocalDateTime dateTime) {
        requireNonNull(dateTime);
        return new Interval(dateTime.toLocalDate(), dateTime.toLocalTime(), endDate, endTime);
    }
746779
public static function modifyNow($interval, $format = self::DEFAULT_TIME_FORMAT)
    {
        return Clock::now()->toDateTime()->modify($interval)->format($format);
    }
1138768
protected function dateIntervalToSeconds($interval): int
    {
        $now = time();
        $dt = new \DateTime();
        $dt->add($interval);
        $ts = $dt->getTimestamp();
        $diff = $ts - $now;
        return $diff;
    }
170568
final public static function formatInterval($interval, $parts = 0)
    {
        $interval = self::recalculateInterval($interval);

        $format = [];
        if ($interval->y !== 0) {
            $format[] = Yii::t(
                'datetime',
                '{y,plural,one{# year} few{# years} many{# years} other{# years}}',
                ['y' => $interval->y]
            );
        }

        if ($interval->m !== 0) {
            $format[] = Yii::t(
                'datetime',
                '{m,plural,one{# month} few{# months} many{# months} other{# months}}',
                ['m' => $interval->m]
            );
        }

        if ($interval->d !== 0) {
            $format[] = Yii::t(
                'datetime',
                '{d,plural,one{# day} few{# days} many{# days} other{# days}}',
                ['d' => $interval->d]
            );
        }

        if ($interval->h !== 0) {
            $format[] = Yii::t(
                'datetime',
                '{h,plural,one{# hour} few{# hours} many{# hours} other{# hours}}',
                ['h' => $interval->h]
            );
        }

        if ($interval->i !== 0) {
            $format[] = Yii::t(
                'datetime',
                '{i,plural,one{#minute} few{# minutes} many{# minutes} other{# minutes}}',
                ['i' => $interval->i]
            );
        }

        if ($interval->s !== 0) {
            $format[] = Yii::t(
                'datetime',
                '{s,plural,one{# second} few{# seconds} many{# seconds} other{# seconds}}',
                ['s' => $interval->s]
            );
        }

        if (count($format) === 0) {
            return "";
        }

        if (($parts > 0) && (count($format) > 1)) {
            $values = [];
            while ((count($values) < $parts) && (count($format) > 0)) {
                $values[] = array_shift($format);
            }
        } else {
            $values = $format;
        }

        return implode(" ", $values);
    }
927938
private static function resolveDateInterval($interval) : \DateInterval
    {
        if ($interval instanceof DateInterval) {
            return $interval->toInternalDateInterval();
        }
        
        if (is_string($interval)) {
            return new \DateInterval($interval);
        }
        
        if ($interval instanceof \DateInterval) {
            return $interval;
        }
        
        throw new \InvalidArgumentException('Interval must be a string or an instance of DateInterval.');
    }
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;
	}
494882
public static function normalize($interval)
    {
        $from = new \DateTime("1970-01-01 00:00:00");
        $to = clone $from;
        $to = $to->add($interval);
        $result = $from->diff($to);

        return ($result !== false) ? $result : $interval;
    }