setTimestamp($timestamp); } elseif (is_string($datetime)) { $Y = $H = $Z = null; if (preg_match(_utils::DMY_PATTERN, $datetime, $ms)) { $Y = $ms[3] ?? null; if ($Y !== null) $Y = _utils::fix_any_year(intval($Y)); else $Y = intval(date("Y")); $m = intval($ms[2]); $d = intval($ms[1]); } elseif (preg_match(_utils::YMD_PATTERN, $datetime, $ms)) { $Y = $ms[1]; if (strlen($Y) == 2) $Y = _utils::fix_any_year(intval($ms[1])); else $Y = intval($Y); $m = intval($ms[2]); $d = intval($ms[3]); } elseif (preg_match(_utils::DMYHIS_PATTERN, $datetime, $ms)) { $Y = $ms[3]; if ($Y !== null) $Y = _utils::fix_any_year(intval($Y)); else $Y = intval(date("Y")); $m = intval($ms[2]); $d = intval($ms[1]); $H = intval($ms[4]); $M = intval($ms[5]); $S = intval($ms[6] ?? 0); } elseif (preg_match(_utils::YMDHISZ_PATTERN, $datetime, $ms)) { $Y = $ms[1]; if (strlen($Y) == 2) $Y = _utils::fix_any_year(intval($ms[1])); else $Y = intval($Y); $m = intval($ms[2]); $d = intval($ms[3]); $H = intval($ms[4]); $M = intval($ms[5]); $S = intval($ms[6] ?? 0); $Z = $ms[7] ?? null; } if ($Y !== null) { if ($H === null) $datetime = sprintf("%04d-%02d-%02d", $Y, $m, $d); else $datetime = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $Y, $m, $d, $H, $M, $S); if ($Z !== null) $timezone = new DateTimeZone(_utils::fix_z($Z)); } $datetime = new \DateTime($datetime, $timezone); } elseif (is_array($datetime) && ($datetime = _utils::parse_array($datetime)) !== null) { [$Y, $m, $d, $H, $M, $S, $Z] = $datetime; if ($H === null && $M === null && $S === null) { $datetime = sprintf("%04d-%02d-%02d", $Y, $m, $d); } else { $datetime = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $Y, $m, $d, $H ?? 0, $M ?? 0, $S ?? 0); } if ($Z !== null) $timezone = new DateTimeZone(_utils::fix_z($Z)); $datetime = new \DateTime($datetime, $timezone); } if ($datetime instanceof DateTimeInterface) { if ($resetTimezone !== null) $datetime->setTimezone($resetTimezone); $datetime = $this->fix($datetime); parent::__construct($datetime->format("Y-m-d\\TH:i:s.uP")); } else { throw new InvalidArgumentException("datetime must be a string or an instance of DateTimeInterface"); } } protected function fix(DateTimeInterface $datetime): DateTimeInterface { return $datetime; } /** @return MutableDateTime|self */ function clone(bool $mutable=false): DateTimeInterface { if ($mutable) return new MutableDateTime($this); else return clone $this; } /** * modifier cet objet pour que l'heure soit à 00:00:00 ce qui le rend propice * à l'utilisation comme borne inférieure d'une période */ function getStartOfDay(): self { return new static($this->setTime(0, 0)); } /** * modifier cet objet pour que l'heure soit à 23:59:59.999999 ce qui le rend * propice à l'utilisation comme borne supérieure d'une période */ function getEndOfDay(): self { return new static($this->setTime(23, 59, 59, 999999)); } function getPrevDay(int $nbDays=1, bool $skipWeekend=false): self { if ($nbDays == 1 && $skipWeekend && $this->wday == 1) { $nbDays = 3; } return new static($this->sub(new \DateInterval("P${nbDays}D"))); } function getNextDay(int $nbDays=1, bool $skipWeekend=false): self { if ($nbDays == 1 && $skipWeekend) { $wday = $this->wday; if ($wday > 5) $nbDays = 8 - $this->wday; } return new static($this->add(new \DateInterval("P${nbDays}D"))); } }