-
Notifications
You must be signed in to change notification settings - Fork 3k
/
datetimeimmutable.php
117 lines (99 loc) · 2.58 KB
/
datetimeimmutable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
class DateTimeImmutable implements DateTimeInterface {
public function __construct(
string $time = "now",
DateTimeZone $timezone = null
) {
$this->data = new DateTime($time, $timezone);
}
public function add(DateInterval $interval): DateTimeImmutable {
$out = clone $this;
$out->data->add($interval);
return $out;
}
public function modify(string $modify): DateTimeImmutable {
$out = clone $this;
$out->data->modify($modify);
return $out;
}
public function setDate(int $year, int $month, int $day): DateTimeImmutable {
$out = clone $this;
$out->data->setDate($year, $month, $day);
return $out;
}
public function setISODate(
int $year,
int $week,
int $day = 1
): DateTimeImmutable {
$out = clone $this;
$out->data->setISODate($year, $week, $day);
return $out;
}
public function setTime(
int $hour,
int $minute,
int $second = 0
): DateTimeImmutable {
$out = clone $this;
$out->data->setTime($hour, $minute, $second);
return $out;
}
public function setTimestamp(int $unixtimestamp): DateTimeImmutable {
$out = clone $this;
$out->data->setTimestamp($unixtimestamp);
return $out;
}
public function setTimezone(DateTimeZone $timezone): DateTimeImmutable {
$out = clone $this;
$out->data->setTimezone($timezone);
return $out;
}
public function sub(DateInterval $interval): DateTimeImmutable {
$out = clone $this;
$out->data->sub($interval);
return $out;
}
public function diff(
$datetime2,
$absolute = false
) {
return $this->data->diff($datetime2, $absolute);
}
public function format($format) {
return $this->data->format($format);
}
public function getOffset() {
return $this->data->getOffset();
}
public function getTimestamp() {
return $this->data->getTimestamp();
}
public function getTimezone() {
return $this->data->getTimezone();
}
public static function createFromFormat(
string $format,
string $time,
DateTimeZone $timezone = null
): mixed {
$out = new DateTimeImmutable();
$out->data = DateTime::createFromFormat($format, $time, $timezone);
if ($out->data === false) {
return false;
}
return $out;
}
public static function createFromMutable(DateTime $datetime) {
$out = new DateTimeImmutable();
$out->data = clone $datetime;
return $out;
}
public static function getLastErrors(): array {
return DateTime::getLastErrors();
}
public function __clone() {
$this->data = clone $this->data;
}
private DateTime $data;
}