@@ -27,20 +27,18 @@ class Date implements Stringable, DialectString
27
27
const WEEKDAY_FRIDAY = 5 ;
28
28
const WEEKDAY_SATURDAY = 6 ;
29
29
const WEEKDAY_SUNDAY = 0 ; // because strftime('%w') is 0 on Sunday
30
-
31
- protected $ string = null ;
32
- protected $ int = null ;
33
-
34
- protected $ year = null ;
35
- protected $ month = null ;
36
- protected $ day = null ;
37
-
30
+
31
+ /**
32
+ * @var DateTime
33
+ */
34
+ protected $ dateTime = null ;
35
+
38
36
/**
39
37
* @return Date
40
38
**/
41
39
public static function create ($ date )
42
40
{
43
- return new self ($ date );
41
+ return new static ($ date );
44
42
}
45
43
46
44
public static function today ($ delimiter = '- ' )
@@ -53,7 +51,7 @@ public static function today($delimiter = '-')
53
51
**/
54
52
public static function makeToday ()
55
53
{
56
- return new self ( self ::today ());
54
+ return new static ( static ::today ());
57
55
}
58
56
59
57
/**
@@ -68,13 +66,13 @@ public static function makeFromWeek($weekNumber, $year = null)
68
66
69
67
Assert::isTrue (
70
68
($ weekNumber > 0 )
71
- && ($ weekNumber <= self ::getWeekCountInYear ($ year ))
69
+ && ($ weekNumber <= static ::getWeekCountInYear ($ year ))
72
70
);
73
71
74
72
$ date =
75
- new self (
73
+ new static (
76
74
date (
77
- self ::getFormat (),
75
+ static ::getFormat (),
78
76
mktime (
79
77
0 , 0 , 0 , 1 , 1 , $ year
80
78
)
@@ -85,7 +83,7 @@ public static function makeFromWeek($weekNumber, $year = null)
85
83
(
86
84
(
87
85
$ weekNumber - 1
88
- + (self ::getWeekCountInYear ($ year - 1 ) == 53 ? 1 : 0 )
86
+ + (static ::getWeekCountInYear ($ year - 1 ) == 53 ? 1 : 0 )
89
87
)
90
88
* 7
91
89
) + 1 - $ date ->getWeekDay ();
@@ -110,10 +108,10 @@ public static function dayDifference(Date $left, Date $right)
110
108
111
109
public static function compare (Date $ left , Date $ right )
112
110
{
113
- if ($ left ->int == $ right ->int )
111
+ if ($ left ->toStamp () == $ right ->toStamp () )
114
112
return 0 ;
115
113
else
116
- return ($ left ->int > $ right ->int ? 1 : -1 );
114
+ return ($ left ->toStamp () > $ right ->toStamp () ? 1 : -1 );
117
115
}
118
116
119
117
public static function getWeekCountInYear ($ year )
@@ -129,77 +127,66 @@ public static function getWeekCountInYear($year)
129
127
130
128
public function __construct ($ date )
131
129
{
132
- if (is_int ($ date ) || is_numeric ($ date )) { // unix timestamp
133
- $ this ->string = date ($ this ->getFormat (), $ date );
134
- } elseif ($ date && is_string ($ date ))
135
- $ this ->stringImport ($ date );
136
-
137
- if ($ this ->string === null ) {
138
- throw new WrongArgumentException (
139
- "strange input given - ' {$ date }' "
140
- );
141
- }
142
-
143
- $ this ->import ($ this ->string );
144
- $ this ->buildInteger ();
130
+ $ this ->import ($ date );
145
131
}
146
132
147
- public function __sleep ()
133
+ public function __clone ()
148
134
{
149
- return array ( ' int ' ) ;
135
+ $ this -> dateTime = clone $ this -> dateTime ;
150
136
}
151
-
152
- public function __wakeup ()
137
+
138
+ public function __sleep ()
153
139
{
154
- $ this -> import ( date ( $ this -> getFormat (), $ this -> int ) );
140
+ return array ( ' dateTime ' );
155
141
}
156
-
142
+
157
143
public function toStamp ()
158
144
{
159
- return $ this ->int ;
145
+ return $ this ->getDateTime ()-> getTimestamp () ;
160
146
}
161
147
162
148
public function toDate ($ delimiter = '- ' )
163
149
{
164
150
return
165
- $ this ->year
151
+ $ this ->getYear ()
166
152
.$ delimiter
167
- .$ this ->month
153
+ .$ this ->getMonth ()
168
154
.$ delimiter
169
- .$ this ->day ;
155
+ .$ this ->getDay () ;
170
156
}
171
157
172
158
public function getYear ()
173
159
{
174
- return $ this ->year ;
160
+ return $ this ->dateTime -> format ( ' Y ' ) ;
175
161
}
176
162
177
163
public function getMonth ()
178
164
{
179
- return $ this ->month ;
165
+ return $ this ->dateTime -> format ( ' m ' ) ;
180
166
}
181
167
182
168
public function getDay ()
183
169
{
184
- return $ this ->day ;
170
+ return $ this ->dateTime -> format ( ' d ' ) ;
185
171
}
186
172
187
173
public function getWeek ()
188
174
{
189
- return date ('W ' , $ this ->int );
175
+ return date ('W ' , $ this ->dateTime -> getTimestamp () );
190
176
}
191
177
192
178
public function getWeekDay ()
193
179
{
194
- return strftime ('%w ' , $ this ->int );
180
+ return strftime ('%w ' , $ this ->dateTime -> getTimestamp () );
195
181
}
196
182
197
183
/**
198
184
* @return Date
199
185
**/
200
186
public function spawn ($ modification = null )
201
187
{
202
- $ child = new $ this ($ this ->string );
188
+
189
+ $ child = new static ($ this ->toString ());
203
190
204
191
if ($ modification )
205
192
return $ child ->modify ($ modification );
@@ -214,17 +201,8 @@ public function spawn($modification = null)
214
201
public function modify ($ string )
215
202
{
216
203
try {
217
- $ time = strtotime ($ string , $ this ->int );
218
-
219
- if ($ time === false )
220
- throw new WrongArgumentException (
221
- "modification yielded false ' {$ string }' "
222
- );
223
-
224
- $ this ->int = $ time ;
225
- $ this ->string = date ($ this ->getFormat (), $ time );
226
- $ this ->import ($ this ->string );
227
- } catch (BaseException $ e ) {
204
+ $ this ->dateTime ->modify ($ string );
205
+ } catch (Exception $ e ) {
228
206
throw new WrongArgumentException (
229
207
"wrong time string ' {$ string }' "
230
208
);
@@ -238,9 +216,9 @@ public function getDayStartStamp()
238
216
return
239
217
mktime (
240
218
0 , 0 , 0 ,
241
- $ this ->month ,
242
- $ this ->day ,
243
- $ this ->year
219
+ $ this ->getMonth () ,
220
+ $ this ->getDay () ,
221
+ $ this ->getYear ()
244
222
);
245
223
}
246
224
@@ -249,9 +227,9 @@ public function getDayEndStamp()
249
227
return
250
228
mktime (
251
229
23 , 59 , 59 ,
252
- $ this ->month ,
253
- $ this ->day ,
254
- $ this ->year
230
+ $ this ->getMonth () ,
231
+ $ this ->getDay () ,
232
+ $ this ->getYear ()
255
233
);
256
234
}
257
235
@@ -277,12 +255,12 @@ public function getLastDayOfWeek($weekStart = Date::WEEKDAY_MONDAY)
277
255
278
256
public function toString ()
279
257
{
280
- return $ this ->string ;
258
+ return $ this ->dateTime -> format ( static :: getFormat ()) ;
281
259
}
282
260
283
261
public function toFormatString ($ format )
284
262
{
285
- return date ( $ format , $ this ->toStamp () );
263
+ return $ this ->dateTime -> format ( $ format );
286
264
}
287
265
288
266
public function toDialectString (Dialect $ dialect )
@@ -306,57 +284,53 @@ public function toTimestamp()
306
284
{
307
285
return Timestamp::create ($ this ->toStamp ());
308
286
}
287
+
288
+ /**
289
+ * @return DateTime|null
290
+ */
291
+ public function getDateTime ()
292
+ {
293
+ return $ this ->dateTime ;
294
+ }
309
295
310
296
protected static function getFormat ()
311
297
{
312
298
return 'Y-m-d ' ;
313
299
}
314
-
315
- /* void */ protected function import ($ string )
300
+
301
+
302
+ protected function import ($ date )
316
303
{
317
- list ($ this ->year , $ this ->month , $ this ->day ) =
318
- explode ('- ' , $ string , 3 );
319
-
320
- if (!$ this ->month || !$ this ->day )
304
+ try {
305
+ if (is_int ($ date ) || is_numeric ($ date )) { // unix timestamp
306
+ $ this ->dateTime = new DateTime (date (static ::getFormat (), $ date ));
307
+
308
+ } elseif ($ date && is_string ($ date )) {
309
+
310
+ if (
311
+ preg_match ('/^(\d{1,4})[-\.](\d{1,2})[-\.](\d{1,2})/ ' , $ date , $ matches )
312
+ ) {
313
+ Assert::isTrue (
314
+ checkdate ($ matches [2 ], $ matches [3 ], $ matches [1 ])
315
+ );
316
+ } elseif (
317
+ preg_match ('/^(\d{1,2})[-\.](\d{1,2})[-\.](\d{1,4})/ ' , $ date , $ matches )
318
+ ) {
319
+ Assert::isTrue (
320
+ checkdate ($ matches [2 ], $ matches [2 ], $ matches [3 ])
321
+ );
322
+ }
323
+
324
+ $ this ->dateTime = new DateTime ($ date );
325
+ }
326
+
327
+
328
+ } catch (Exception $ e ) {
321
329
throw new WrongArgumentException (
322
- 'month and day must not be zero '
323
- );
324
-
325
- $ this ->string =
326
- sprintf (
327
- '%04d-%02d-%02d ' ,
328
- $ this ->year ,
329
- $ this ->month ,
330
- $ this ->day
331
- );
332
-
333
- list ($ this ->year , $ this ->month , $ this ->day ) =
334
- explode ('- ' , $ this ->string , 3 );
335
- }
336
-
337
- /* void */ protected function stringImport ($ string )
338
- {
339
- $ matches = array ();
340
-
341
- if (
342
- preg_match ('/^(\d{1,4})-(\d{1,2})-(\d{1,2})$/ ' , $ string , $ matches )
343
- ) {
344
- if (checkdate ($ matches [2 ], $ matches [3 ], $ matches [1 ]))
345
- $ this ->string = $ string ;
346
-
347
- } elseif (($ stamp = strtotime ($ string )) !== false )
348
- $ this ->string = date ($ this ->getFormat (), $ stamp );
349
- }
350
-
351
- /* void */ protected function buildInteger ()
352
- {
353
- $ this ->int =
354
- mktime (
355
- 0 , 0 , 0 ,
356
- $ this ->month ,
357
- $ this ->day ,
358
- $ this ->year
330
+ "strange input given - ' {$ date }' "
359
331
);
332
+ }
333
+
360
334
}
361
335
}
362
336
?>
0 commit comments