/
DateTime.php
288 lines (256 loc) · 7.81 KB
/
DateTime.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
declare( strict_types=1 );
namespace ExtDateTime;
use DateInterval;
use DateTimeInterface;
use DateTimeZone;
use Exception;
/**
* Class DateTime
*
* @package ExtDateTime
*/
class DateTime extends \DateTime
{
/**
* Returns the current server date time or `false` if the current day could not be fetched.
*
* @return boolean|static
*
* @throws Exception
*/
public static function current()
{
return static::createFromFormat('U.u', sprintf('%.6F', microtime(true)));
}
/**
* Parses a string into a new {@see DateTime} object according to the specified format.
*
* @param string $format Format accepted by date().
* @param string $time String representing the time.
* @param DateTimeZone|null $timezone [optional] A DateTimeZone object representing the desired time zone.
*
* @return boolean|static
*
* @throws Exception
*
* @link http://php.net/manual/en/datetime.createfromformat.php
*/
public static function createFromFormat($format, $time, DateTimeZone $timezone = null)
{
$datetime = is_null($timezone) ?
parent::createFromFormat($format, $time) :
parent::createFromFormat($format, $time, $timezone);
if( !is_object($datetime) )
{
throw new Exception('Cannot create an object by DateTime::createFromFormat().');
}
return static::createFromObject($datetime);
}
/**
* Overrides {@see \DateTime::createFromImmutable()} to return a {@see \ExtDateTime\DateTime} object
* instead of the default {@see \DateTime} object.
*
* @param \DateTimeImmutable $DateTimeImmutable
*
* @return boolean|static
*
* @throws Exception
*/
public static function createFromImmutable($DateTimeImmutable)
{
return static::createFromObject($DateTimeImmutable);
}
/**
* Copies and casts the submitted datetime object to a new {@see \ExtDateTime\DateTime} object.
*
* @param DateTimeInterface $dateTime the source object has to implement the {@see \DateTimeInterface}
*
* @return boolean|static
*/
public static function createFromObject(DateTimeInterface $dateTime)
{
$parts = explode(':', serialize($dateTime));
$parts[ 1 ] = strlen(static::class);
$parts[ 2 ] = sprintf('"%s"', static::class);
return unserialize(implode(':', $parts));
}
/**
* Static constructor. Returns a new {@see \ExtDateTime\DateTime} object.
*
* @param string $time
* @param DateTimeZone $timezone
*
* @return boolean|static
*
* @throws Exception
*/
public static function create(string $time = 'now', DateTimeZone $timezone = null)
{
return new static($time, $timezone);
}
/**
* Converts the mutable datetime object to an immutable datetime object.
*
* Wrapper function of {\ExtDateTime\DateTimeImmutable::createFromMutable()}.
*
* @return DateTimeImmutable
*
* @throws Exception
*/
public function toImmutable(): DateTimeImmutable
{
return DateTimeImmutable::createFromMutable($this);
}
/**
* Returns a cloned object.
*
* This method is just a wrapper function for {@see \clone()}.
*
* @return static
*/
public function duplicate(): DateTimeInterface
{
return clone $this;
}
/**
* @return integer
*/
protected function getAbsoluteMonths(): int
{
return intval($this->format('Y')) * 12 + intval($this->format('m'));
}
/**
* Adds (or subtracts) hours.
*
* @param integer $hours to add some hours pass a positive number, to subtract pass a negative number
*
* @return static
*/
public function addHours(int $hours): DateTimeInterface
{
return $this->add(DateInterval::createFromDateString(sprintf('%d hour', $hours)));
}
/**
* Subtracts (or adds) hours.
*
* @param integer $hours to subtract some hours pass a positive number, to add pass a negative number
*
* @return static
*/
public function subHours(int $hours): DateTimeInterface
{
return $this->addHours(0 - $hours);
}
/**
* Adds (or subtracts) days.
*
* @param integer $days to add some days pass a positive number, to subtract pass a negative number
*
* @return static
*/
public function addDays(int $days): DateTimeInterface
{
return $this->add(DateInterval::createFromDateString(sprintf('%d day', $days)));
}
/**
* Subtracts (or adds) days.
*
* @param integer $days to subtracts some days pass a positive number, to add pass a negative number
*
* @return static
*/
public function subDays(int $days): DateTimeInterface
{
return $this->addDays(0 - $days);
}
/**
* Adds an amount of months to the current date.
*
* **ATTENTION**: If the current date is the 31/30/29 and the target month has less days then the current month, the day will be set to the last
* day of the target month.
*
* Example: Current date is '2017-01-30 17:00:00' and you want to add '1 month', then the result will be '2017-02-28 17:00:00'.
*
* @param integer $month
*
* @return static
*/
public function addMonth(int $month): DateTimeInterface
{
$absoluteMonths = $this->getAbsoluteMonths();
$this->add(DateInterval::createFromDateString(sprintf('%d month', $month)));
if( $absoluteMonths + $month !== $this->getAbsoluteMonths() )
{
$this->subDays((int) $this->format('d'));
}
return $this;
}
/**
* Subs an amount of months from the current date.
*
* **ATTENTION**: If the current date is the 31/30/29 and the target month has less days then the current month, the day will be set to the last
* day of the target month.
*
* Example: Current date is '2017-03-31 17:00:00' and you want to sub '1 month', then the result will be '2017-02-28 17:00:00'.
*
* @param integer $month
*
* @return static
*/
public function subMonth(int $month): DateTimeInterface
{
$absolute_months = $this->getAbsoluteMonths();
$this->sub(DateInterval::createFromDateString(sprintf('%d month', $month)));
if( $absolute_months - $month !== $this->getAbsoluteMonths() )
{
$this->subDays((int) $this->format('d'));
}
return $this;
}
/**
* Sets the time to the end of the day (`23:59:59`).
*
* @return static
*/
public function toEndOfDay(): DateTimeInterface
{
return $this->setTime(23, 59, 59);
}
/**
* Sets the time to noon (`12:00:00`).
*
* @return static
*/
public function toNoon(): DateTimeInterface
{
return $this->setTime(12, 0, 0);
}
/**
* Sets the time to the start of the day (`00:00:00`).
*
* @return static
*/
public function toStartOfDay(): DateTimeInterface
{
return $this->setTime(0, 0, 0);
}
/**
* Sets the date to the first day of the month and additionally the time to the start of the day (`00:00:00`).
*
* @return static
*/
public function toStartOfMonth(): DateTimeInterface
{
return $this->setDate((int) $this->format('Y'), (int) $this->format('m'), 1)->toStartOfDay();
}
/**
* Sets the date to the last day of the month and additionally the time to the end of the day (`23:59:59`).
*
* @return static
*/
public function toEndOfMonth(): DateTimeInterface
{
return $this->setDate((int) $this->format('Y'), (int) $this->format('m'), (int) $this->format('t'))->toEndOfDay();
}
}