2018-02-26 10:45:25 -05:00
|
|
|
<?php
|
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2018 J. King et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace JKingWeb\Lax;
|
|
|
|
|
|
|
|
class Date extends \DateTimeImmutable implements \JsonSerializable {
|
2018-02-26 11:10:37 -05:00
|
|
|
static public $supportedFormats = [
|
|
|
|
"Y-m-d\TH:i:s.u O",
|
|
|
|
"Y-m-d\TH:i:s.uO",
|
|
|
|
"Y-m-d\TH:i:s.u P",
|
|
|
|
"Y-m-d\TH:i:s.uP",
|
|
|
|
"Y-m-d\TH:i:s.u T",
|
|
|
|
"Y-m-d\TH:i:s.u\Z",
|
|
|
|
"Y-m-d\TH:i:s.u",
|
|
|
|
"Y-m-d H:i:s.u O",
|
|
|
|
"Y-m-d H:i:s.uO",
|
|
|
|
"Y-m-d H:i:s.u P",
|
|
|
|
"Y-m-d H:i:s.uP",
|
|
|
|
"Y-m-d H:i:s.u T",
|
|
|
|
"Y-m-d H:i:s.u\Z",
|
|
|
|
"Y-m-d H:i:s.u",
|
|
|
|
"Y-m-d\TH:i:s O",
|
|
|
|
"Y-m-d\TH:i:sO",
|
|
|
|
"Y-m-d\TH:i:s P",
|
|
|
|
"Y-m-d\TH:i:sP",
|
|
|
|
"Y-m-d\TH:i:s T",
|
|
|
|
"Y-m-d\TH:i:s",
|
|
|
|
"Y-m-d H:i:s O",
|
|
|
|
"Y-m-d H:i:sO",
|
|
|
|
"Y-m-d H:i:s P",
|
|
|
|
"Y-m-d H:i:sP",
|
|
|
|
"Y-m-d H:i:s T",
|
|
|
|
"Y-m-d H:i:s\Z",
|
|
|
|
"Y-m-d H:i:s",
|
|
|
|
"D, d M Y H:i:s O",
|
|
|
|
"D, d M Y H:i:sO",
|
|
|
|
"D, d M Y H:i:s P",
|
|
|
|
"D, d M Y H:i:sP",
|
|
|
|
"D, d M Y H:i:s T",
|
|
|
|
"D, d M Y H:i:s",
|
|
|
|
"D, d M y H:i:s O",
|
|
|
|
"D, d M y H:i:sO",
|
|
|
|
"D, d M y H:i:s P",
|
|
|
|
"D, d M y H:i:sP",
|
|
|
|
"D, d M y H:i:s T",
|
|
|
|
"D, d M y H:i:s",
|
2018-02-26 10:45:25 -05:00
|
|
|
];
|
|
|
|
|
2020-02-27 09:24:30 -05:00
|
|
|
protected static function create(\DateTimeInterface $temp): self {
|
2018-02-26 10:45:25 -05:00
|
|
|
if (version_compare(\PHP_VERSION, "7.1", ">=")) {
|
|
|
|
return (new self)->setTimestamp($temp->getTimestamp())->setTimezone($temp->getTimezone())->setTime((int) $temp->format("H"), (int) $temp->format("i"), (int) $temp->format("s"), (int) $temp->format("u"));
|
|
|
|
} else {
|
|
|
|
return (new self)->setTimestamp($temp->getTimestamp())->setTimezone($temp->getTimezone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct($time = "now", $timezone = null) {
|
|
|
|
parent::__construct($time, $timezone);
|
|
|
|
}
|
|
|
|
|
2020-02-27 09:24:30 -05:00
|
|
|
public static function createFromFormat($format, $time, $timezone = null): ?self {
|
2018-02-26 11:10:37 -05:00
|
|
|
$temp = parent::createFromFormat("!".$format, $time, $timezone);
|
2020-02-27 09:24:30 -05:00
|
|
|
return $temp ? static::create($temp) : null;
|
2018-02-26 10:45:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function createFromMutable($datetime) {
|
|
|
|
$temp = parent::createFromMutable($datetime);
|
2020-02-27 09:24:30 -05:00
|
|
|
return $temp ? static::create($temp) : null;
|
2018-02-26 10:45:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function createFromImmutable($datetime) {
|
|
|
|
$temp = \DateTime::createFromImmutable($datetime);
|
2020-02-27 09:24:30 -05:00
|
|
|
return $temp ? static::create($temp) : null;
|
2018-02-26 10:45:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __toString() {
|
|
|
|
if ((int) $this->format("u")) {
|
|
|
|
return $this->format("Y-m-d\TH:i:s.uP");
|
|
|
|
} else {
|
|
|
|
return $this->format("Y-m-d\TH:i:sP");
|
|
|
|
}
|
|
|
|
}
|
2018-02-27 09:30:56 -05:00
|
|
|
|
2018-02-26 10:45:25 -05:00
|
|
|
public function jsonSerialize() {
|
|
|
|
return $this->__toString();
|
|
|
|
}
|
|
|
|
}
|