<?php
// CBPCalc :: FunctionDateAdd()
// /dist/bitrix/modules/bizproc/classes/general/calc.php:554
private function FunctionDateAdd($args)
{
if (!is_array($args))
$args = [$args];
$ar = $this->ArrgsToArray($args);
$date = array_shift($ar);
$offset = $this->getDateTimeOffset($date);
$interval = array_shift($ar);
if (($date = $this->makeTimestamp($date)) === false)
return null;
if (empty($interval))
return $date;
// 1Y2M3D4H5I6S, -4 days 5 hours, 1month, 5h
$interval = trim($interval);
$bMinus = false;
if (substr($interval, 0, 1) === "-")
{
$interval = substr($interval, 1);
$bMinus = true;
}
static $arMap = ["y" => "YYYY", "year" => "YYYY", "years" => "YYYY",
"m" => "MM", "month" => "MM", "months" => "MM",
"d" => "DD", "day" => "DD", "days" => "DD",
"h" => "HH", "hour" => "HH", "hours" => "HH",
"i" => "MI", "min" => "MI", "minute" => "MI", "minutes" => "MI",
"s" => "SS", "sec" => "SS", "second" => "SS", "seconds" => "SS",
];
$arInterval = [];
while (preg_match('/\s*([\d]+)\s*([a-z]+)\s*/i', $interval, $match))
{
$match2 = strtolower($match[2]);
if (array_key_exists($match2, $arMap))
$arInterval[$arMap[$match2]] = ($bMinus ? -intval($match[1]) : intval($match[1]));
$p = strpos($interval, $match[0]);
$interval = substr($interval, $p + strlen($match[0]));
}
$newDate = AddToTimeStamp($arInterval, $date);
return new Bizproc\BaseType\Value\DateTime($newDate, $offset);
}