21 $this->value = new \DateTime();
22 if ($date !==
null && $date !==
"")
26 $format = static::getFormat();
29 $parsedValue = $this->
parse($format, $date);
31 if($parsedValue ===
false)
36 if(isset($parsedValue[
"timestamp"]))
38 $this->value->setTimestamp($parsedValue[
"timestamp"]);
42 $this->value->setDate($parsedValue[
'year'], $parsedValue[
'month'], $parsedValue[
'day']);
45 $this->value->setTime(0, 0);
53 protected function parse($format, $time)
55 $parsedValue = date_parse_from_format($format, $time);
59 if ($parsedValue[
'error_count'] > 1)
61 $error = current($parsedValue[
'errors']);
63 if ($error ===
'A two digit second could not be found')
66 $timestamp = strtotime($time);
68 if ($timestamp ===
false)
74 "timestamp" => $timestamp,
77 if ($error !==
'Trailing data' && $error !==
'Data missing')
83 if(isset($parsedValue[
"relative"][
"second"]) && $parsedValue[
"relative"][
"second"] <> 0)
86 "timestamp" => $parsedValue[
"relative"][
"second"],
91 if($parsedValue[
'month'] ===
false)
93 $parsedValue[
'month'] = 1;
95 if($parsedValue[
'day'] ===
false)
97 $parsedValue[
'day'] = 1;
112 return $this->value->format($format);
145 public function add($interval)
147 $i = $this->tryToCreateIntervalByDesignators($interval);
150 $i = \DateInterval::createFromDateString($interval);
153 $this->value->add($i);
168 $this->value->setDate($year, $month, $day);
173 private function tryToCreateIntervalByDesignators($interval)
175 if (!is_string($interval) || strpos($interval,
' ') !==
false)
183 $intervalTmp = strtoupper($interval);
185 $firstChar = substr($intervalTmp, 0, 1);
186 if ($firstChar ===
"-")
189 $intervalTmp = substr($intervalTmp, 1);
190 $firstChar = substr($intervalTmp, 0, 1);
193 if ($firstChar !==
"P")
195 $intervalTmp =
"P".$intervalTmp;
197 $i = new \DateInterval($intervalTmp);
203 catch (\Exception $e)
217 return $this->value->getTimestamp();
228 return $this->value->diff($time->value);
240 $format = static::getFormat($culture);
241 return $this->
format($format);
263 static $defaultCulture =
null;
265 if($culture ===
null)
267 if($defaultCulture ===
null)
272 $defaultCulture = $context->getCulture();
275 $culture = $defaultCulture;
278 $format = static::getCultureFormat($culture);
280 return static::convertFormatToPhp($format);
294 return $culture->getDateFormat();
308 static $from = array(
329 $format = str_replace($from, $to, $format);
331 $tempFormat = $format;
332 $format = str_replace(
"HH",
"H", $format);
333 if ($tempFormat === $format)
335 $format = str_replace(
"H",
"h", $format);
338 $tempFormat = $format;
339 $format = str_replace(
"GG",
"G", $format);
340 if ($tempFormat === $format)
342 $format = str_replace(
"G",
"g", $format);
367 new static($time, $format);
387 $d->value = clone $datetime;
402 $d->value->setTimestamp($timestamp);
403 $d->value->setTime(0, 0);
416 $result = Main\Text\DateConverter::decode($text);
422 return $result[0]->getDate();
static createFromTimestamp($timestamp)
static getFormat(Context\Culture $culture=null)
setDate($year, $month, $day)
toString(Context\Culture $culture=null)
static getCultureFormat(Context\Culture $culture=null)
__construct($date=null, $format=null)
static convertFormatToPhp($format)
static createFromText($text)
static isCorrect($time, $format=null)
static createFromPhp(\DateTime $datetime)
setTime($hour, $minute, $second=0, $microseconds=0)