|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Main Datium class |
|
4
|
|
|
* |
|
5
|
|
|
* @category Core |
|
6
|
|
|
* @package OpenCafe\Datium |
|
7
|
|
|
* @author Mehdi Hosseini <[email protected]> |
|
8
|
|
|
* @license icense https://opensource.org/licenses/MIT |
|
9
|
|
|
* @link https://github.com/opencafe/datium |
|
10
|
|
|
* @since Aug 17, 2015 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace OpenCafe; |
|
14
|
|
|
|
|
15
|
|
|
use DateTime; |
|
16
|
|
|
use DateInterval; |
|
17
|
|
|
use OpenCafe\Tools\Convert; |
|
18
|
|
|
use OpenCafe\Tools\Leap; |
|
19
|
|
|
use OpenCafe\Tools\DayOf; |
|
20
|
|
|
use OpenCafe\Tools\Lang; |
|
21
|
|
|
|
|
22
|
|
|
use OpenCafe\Datium; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Main Datium class |
|
26
|
|
|
* |
|
27
|
|
|
* @category Core |
|
28
|
|
|
* @package OpenCafe\Datium |
|
29
|
|
|
* @author Mehdi Hosseini <[email protected]> |
|
30
|
|
|
* @license icense https://opensource.org/licenses/MIT |
|
31
|
|
|
* @link https://github.com/opencafe/datium |
|
32
|
|
|
* @since Aug 17, 2015 |
|
33
|
|
|
*/ |
|
34
|
|
|
class Datium |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Store DateTime object |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $date_time; |
|
41
|
|
|
|
|
42
|
|
|
protected static $static_date_time; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Store config file statements |
|
46
|
|
|
* |
|
47
|
|
|
* @param array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $config; |
|
50
|
|
|
|
|
51
|
|
|
protected $date_interval_expression; |
|
52
|
|
|
|
|
53
|
|
|
protected static $date_start; |
|
54
|
|
|
|
|
55
|
|
|
protected static $date_end; |
|
56
|
|
|
|
|
57
|
|
|
protected $translate_from; |
|
58
|
|
|
|
|
59
|
|
|
protected $translate_to; |
|
60
|
|
|
|
|
61
|
|
|
protected $toConfig; |
|
62
|
|
|
|
|
63
|
|
|
protected $fromConfig; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Return store day number |
|
67
|
|
|
* |
|
68
|
|
|
* @param integer |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $day_of; |
|
71
|
|
|
|
|
72
|
|
|
protected $leap; |
|
73
|
|
|
|
|
74
|
|
|
protected $events; |
|
75
|
|
|
|
|
76
|
|
|
protected $translate; |
|
77
|
|
|
|
|
78
|
|
|
protected $gregorian_DayofWeek; |
|
79
|
|
|
|
|
80
|
|
|
protected $convert_calendar; |
|
81
|
|
|
|
|
82
|
|
|
protected $calendar_type; |
|
83
|
|
|
|
|
84
|
|
|
protected static $array_date; |
|
85
|
|
|
|
|
86
|
|
|
protected static $call_type; |
|
87
|
|
|
|
|
88
|
|
|
protected $translate_from_file; |
|
89
|
|
|
|
|
90
|
|
|
protected $translate_to_file; |
|
91
|
|
|
|
|
92
|
|
|
protected $language; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Datium class constructure |
|
96
|
|
|
*/ |
|
97
|
|
|
public function __construct() |
|
98
|
|
|
{ |
|
99
|
|
|
|
|
100
|
|
|
$this->language = 'en'; |
|
101
|
|
|
|
|
102
|
|
|
$this->translate_from = 'gregorian'; |
|
103
|
|
|
|
|
104
|
|
|
$this->translate_to = 'gregorian'; |
|
105
|
|
|
|
|
106
|
|
|
$this->config = include 'Config.php'; |
|
107
|
|
|
|
|
108
|
|
|
$this->calendar_type = $this->config[ 'default_calendar' ]; |
|
109
|
|
|
|
|
110
|
|
|
date_default_timezone_set($this->config[ 'timezone' ]); |
|
111
|
|
|
|
|
112
|
|
|
$this->calendar_type = 'gregorian'; |
|
113
|
|
|
|
|
114
|
|
|
switch (Datium::$call_type) { |
|
115
|
|
|
case 'now': |
|
116
|
|
|
$this->date_time = new DateTime('now'); |
|
117
|
|
|
|
|
118
|
|
|
$this->gregorian_DayofWeek = $this->date_time->format('w'); |
|
119
|
|
|
|
|
120
|
|
|
break; |
|
121
|
|
|
|
|
122
|
|
|
case 'make': |
|
123
|
|
|
$this->date_time = new DateTime('now'); |
|
124
|
|
|
|
|
125
|
|
|
$this->date_time->setDate( |
|
126
|
|
|
self::$array_date[ 'year' ], |
|
127
|
|
|
self::$array_date[ 'month' ], |
|
128
|
|
|
self::$array_date[ 'day' ] |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
|
|
$this->date_time->setTime( |
|
132
|
|
|
self::$array_date[ 'hour' ], |
|
133
|
|
|
self::$array_date[ 'minute' ], |
|
134
|
|
|
self::$array_date[ 'second' ] |
|
135
|
|
|
); |
|
136
|
|
|
|
|
137
|
|
|
$this->gregorian_DayofWeek = $this->date_time->format('w'); |
|
138
|
|
|
|
|
139
|
|
|
break; |
|
140
|
|
|
|
|
141
|
|
|
case 'set': |
|
142
|
|
|
$this->date_time = Datium::$static_date_time; |
|
143
|
|
|
|
|
144
|
|
|
$this->gregorian_DayofWeek = $this->date_time->format('w'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$this->convert_calendar = new Convert(); |
|
148
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Return all datetime parts as an object |
|
153
|
|
|
* |
|
154
|
|
|
* @return object |
|
155
|
|
|
*/ |
|
156
|
|
|
public function all() |
|
157
|
|
|
{ |
|
158
|
|
|
|
|
159
|
|
|
return ( object ) array( |
|
160
|
|
|
|
|
161
|
|
|
'second' => $this->date_time->format('s'), |
|
162
|
|
|
|
|
163
|
|
|
'minute' => $this->date_time->format('m'), |
|
164
|
|
|
|
|
165
|
|
|
'hour' => $this->date_time->format('H'), |
|
166
|
|
|
|
|
167
|
|
|
'day' => $this->date_time->format('d'), |
|
168
|
|
|
|
|
169
|
|
|
'month' => $this->date_time->format('m'), |
|
170
|
|
|
|
|
171
|
|
|
'year' => $this->date_time->format('Y') |
|
172
|
|
|
|
|
173
|
|
|
); |
|
174
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Get current datetime |
|
179
|
|
|
* |
|
180
|
|
|
* @since Aug 17 2015 |
|
181
|
|
|
* @return object |
|
182
|
|
|
*/ |
|
183
|
|
|
public static function now() |
|
184
|
|
|
{ |
|
185
|
|
|
|
|
186
|
|
|
self::$call_type = 'now'; |
|
187
|
|
|
|
|
188
|
|
|
return new Datium(); |
|
189
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Create new date time |
|
194
|
|
|
* |
|
195
|
|
|
* @param integer $year Year number |
|
196
|
|
|
* @param integer $month month number |
|
197
|
|
|
* @param integer $day day number |
|
198
|
|
|
* @param integer $hour hour number |
|
199
|
|
|
* @param integer $minute minute number |
|
200
|
|
|
* @param integer $second second number |
|
201
|
|
|
* |
|
202
|
|
|
* @return object |
|
203
|
|
|
*/ |
|
204
|
|
|
public static function create($year = 2000, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0) |
|
205
|
|
|
{ |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* When we want to set a Datetime object to Datium |
|
209
|
|
|
*/ |
|
210
|
|
|
if (func_num_args() === 1) { |
|
211
|
|
|
self::$static_date_time = func_get_arg(0); |
|
212
|
|
|
|
|
213
|
|
|
self::$call_type = 'set'; |
|
214
|
|
|
} else { |
|
215
|
|
|
self::$array_date = array( |
|
216
|
|
|
'year' => $year, |
|
217
|
|
|
'month' => $month, |
|
218
|
|
|
'day' => $day, |
|
219
|
|
|
'hour' => $hour, |
|
220
|
|
|
'minute' => $minute, |
|
221
|
|
|
'second' => $second |
|
222
|
|
|
); |
|
223
|
|
|
|
|
224
|
|
|
self::$call_type = 'make'; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
return new Datium(); |
|
228
|
|
|
|
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Select The range between two date object |
|
233
|
|
|
* |
|
234
|
|
|
* @param object $date_start Start of the DateTime |
|
235
|
|
|
* @param object $date_end End of the DateTime |
|
236
|
|
|
* |
|
237
|
|
|
* @return object |
|
238
|
|
|
*/ |
|
239
|
|
|
public static function between($date_start, $date_end) |
|
240
|
|
|
{ |
|
241
|
|
|
|
|
242
|
|
|
self::$date_start = $date_start; |
|
243
|
|
|
|
|
244
|
|
|
self::$date_end = $date_end; |
|
245
|
|
|
|
|
246
|
|
|
self::$call_type = 'between'; |
|
247
|
|
|
|
|
248
|
|
|
return new Datium(); |
|
249
|
|
|
|
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Convert from current calendar, what type is current calendar? |
|
254
|
|
|
* |
|
255
|
|
|
* @param object $calendar Assigned calendar to start from |
|
256
|
|
|
* |
|
257
|
|
|
* @return $object |
|
|
|
|
|
|
258
|
|
|
*/ |
|
259
|
|
View Code Duplication |
public function from($calendar) |
|
|
|
|
|
|
260
|
|
|
{ |
|
261
|
|
|
|
|
262
|
|
|
$this->convert = new Convert($this->date_time); |
|
|
|
|
|
|
263
|
|
|
|
|
264
|
|
|
$this->date_time = $this->convert->from($calendar); |
|
|
|
|
|
|
265
|
|
|
|
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* We need this part for DayOf class |
|
269
|
|
|
*/ |
|
270
|
|
|
$this->calendar_type = $calendar; |
|
271
|
|
|
|
|
272
|
|
|
$this->translate_to = $calendar; |
|
273
|
|
|
|
|
274
|
|
|
return $this; |
|
275
|
|
|
|
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Convert date to current Date |
|
280
|
|
|
* |
|
281
|
|
|
* @param object $calendar Assigned calendar to when calendar should start. |
|
282
|
|
|
* |
|
283
|
|
|
* @return object |
|
284
|
|
|
*/ |
|
285
|
|
View Code Duplication |
public function to($calendar) |
|
|
|
|
|
|
286
|
|
|
{ |
|
287
|
|
|
|
|
288
|
|
|
$this->convert = new Convert($this->date_time); |
|
|
|
|
|
|
289
|
|
|
|
|
290
|
|
|
$this->date_time = $this->convert->to($calendar); |
|
|
|
|
|
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* We need this part for DayOf class |
|
294
|
|
|
*/ |
|
295
|
|
|
$this->calendar_type = $calendar; |
|
296
|
|
|
|
|
297
|
|
|
$this->translate_to = $calendar; |
|
298
|
|
|
|
|
299
|
|
|
return $this; |
|
300
|
|
|
|
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Difference between two time |
|
306
|
|
|
* |
|
307
|
|
|
* @param DateTime $start Start of the date |
|
308
|
|
|
* @param DateTime $end End of the date |
|
309
|
|
|
* |
|
310
|
|
|
* @return object |
|
311
|
|
|
*/ |
|
312
|
|
|
public static function diff($start, $end) |
|
313
|
|
|
{ |
|
314
|
|
|
|
|
315
|
|
|
return date_diff($start, $end); |
|
316
|
|
|
|
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Add new date value to current date |
|
321
|
|
|
* |
|
322
|
|
|
* @param string $value How much date should be added to current date |
|
323
|
|
|
* |
|
324
|
|
|
* @return object |
|
325
|
|
|
*/ |
|
326
|
|
View Code Duplication |
public function add($value) |
|
|
|
|
|
|
327
|
|
|
{ |
|
328
|
|
|
|
|
329
|
|
|
$this->date_interval_expression = str_replace( |
|
330
|
|
|
$this->config[ 'date_simple' ], |
|
331
|
|
|
$this->config[ 'date_interval' ], |
|
332
|
|
|
$value |
|
333
|
|
|
); |
|
334
|
|
|
|
|
335
|
|
|
$this->date_interval_expression = str_replace( |
|
336
|
|
|
' ', |
|
337
|
|
|
'', |
|
338
|
|
|
'P' . $this->date_interval_expression |
|
339
|
|
|
); |
|
340
|
|
|
|
|
341
|
|
|
$this->date_time->add( |
|
342
|
|
|
new DateInterval($this->date_interval_expression) |
|
343
|
|
|
); |
|
344
|
|
|
|
|
345
|
|
|
return $this; |
|
346
|
|
|
|
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* Sub date from current date |
|
351
|
|
|
* |
|
352
|
|
|
* @param string $value How much date should increase from current date |
|
353
|
|
|
* |
|
354
|
|
|
* @return obejct |
|
355
|
|
|
*/ |
|
356
|
|
View Code Duplication |
public function sub($value) |
|
|
|
|
|
|
357
|
|
|
{ |
|
358
|
|
|
|
|
359
|
|
|
$this->date_interval_expression = str_replace( |
|
360
|
|
|
$this->config[ 'date_simple' ], |
|
361
|
|
|
$this->config[ 'date_interval' ], |
|
362
|
|
|
$value |
|
363
|
|
|
); |
|
364
|
|
|
|
|
365
|
|
|
$this->date_interval_expression = str_replace( |
|
366
|
|
|
' ', |
|
367
|
|
|
'', |
|
368
|
|
|
'P' . $this->date_interval_expression |
|
369
|
|
|
); |
|
370
|
|
|
|
|
371
|
|
|
$this->date_time->sub( |
|
372
|
|
|
new DateInterval($this->date_interval_expression) |
|
373
|
|
|
); |
|
374
|
|
|
|
|
375
|
|
|
return $this; |
|
376
|
|
|
|
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* Check if current year is leap or not |
|
381
|
|
|
* |
|
382
|
|
|
* @param string $type Name of the calendar to caculate leap year |
|
383
|
|
|
* |
|
384
|
|
|
* @return boolean |
|
385
|
|
|
*/ |
|
386
|
|
|
public function leap($type = 'gregorian') |
|
387
|
|
|
{ |
|
388
|
|
|
|
|
389
|
|
|
$this->leap = new Leap($this->date_time->format('Y'), $type); |
|
390
|
|
|
|
|
391
|
|
|
return $this->leap; |
|
392
|
|
|
|
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* Caculate day of year or week |
|
397
|
|
|
* |
|
398
|
|
|
* @since Aug, 22 2015 |
|
399
|
|
|
* |
|
400
|
|
|
* @return integer |
|
401
|
|
|
*/ |
|
402
|
|
|
public function dayOf() |
|
403
|
|
|
{ |
|
404
|
|
|
|
|
405
|
|
|
$this->day_of = new DayOf($this->date_time, $this->calendar_type); |
|
|
|
|
|
|
406
|
|
|
|
|
407
|
|
|
return $this->day_of; |
|
408
|
|
|
|
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
// public function events() |
|
|
|
|
|
|
412
|
|
|
// { |
|
413
|
|
|
// |
|
414
|
|
|
// if (Datium::$call_type == 'between' ) { |
|
|
|
|
|
|
415
|
|
|
// |
|
416
|
|
|
// $this->events = new Events(Datium::$date_start, Datium::$date_end); |
|
|
|
|
|
|
417
|
|
|
// |
|
418
|
|
|
// } else { |
|
|
|
|
|
|
419
|
|
|
// |
|
420
|
|
|
// $this->events = new Events($this->date_time); |
|
|
|
|
|
|
421
|
|
|
// |
|
422
|
|
|
// } |
|
423
|
|
|
// |
|
424
|
|
|
// return $this->events; |
|
|
|
|
|
|
425
|
|
|
// |
|
426
|
|
|
// } |
|
427
|
|
|
|
|
428
|
|
|
/** |
|
429
|
|
|
* Return Datetime as a original object |
|
430
|
|
|
* |
|
431
|
|
|
* @since Oct 22, 2015 |
|
432
|
|
|
* |
|
433
|
|
|
* @return object |
|
434
|
|
|
*/ |
|
435
|
|
|
public function object() |
|
436
|
|
|
{ |
|
437
|
|
|
|
|
438
|
|
|
return $this->date_time; |
|
439
|
|
|
|
|
440
|
|
|
} |
|
441
|
|
|
|
|
442
|
|
|
/** |
|
443
|
|
|
* Translate current date string to selected language |
|
444
|
|
|
* |
|
445
|
|
|
* @param string $language language short name fa, en, ar ... |
|
446
|
|
|
* |
|
447
|
|
|
* @return object |
|
448
|
|
|
*/ |
|
449
|
|
|
public function lang($language = 'fa') |
|
450
|
|
|
{ |
|
451
|
|
|
|
|
452
|
|
|
$this->language = $language; |
|
453
|
|
|
|
|
454
|
|
|
$this->result = new Lang(); |
|
|
|
|
|
|
455
|
|
|
|
|
456
|
|
|
$this->result->setConfig($this->language); |
|
457
|
|
|
|
|
458
|
|
|
$this->toConfig = $this->result->getConfig(); |
|
459
|
|
|
|
|
460
|
|
|
return $this; |
|
461
|
|
|
|
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
/** |
|
465
|
|
|
* Return fainal result |
|
466
|
|
|
* |
|
467
|
|
|
* @param string $format Date format |
|
468
|
|
|
* |
|
469
|
|
|
* @since Aug 17 2015 |
|
470
|
|
|
* |
|
471
|
|
|
* @return string |
|
472
|
|
|
*/ |
|
473
|
|
|
public function get($format = 'Y-m-d H:i:s') |
|
474
|
|
|
{ |
|
475
|
|
|
|
|
476
|
|
|
// $this->translate_from_file = include( 'Lang/en/general.php' ); |
|
|
|
|
|
|
477
|
|
|
// |
|
478
|
|
|
// $this->translate_to_file = include( 'Lang/' . $this->language . '/general.php' ); |
|
|
|
|
|
|
479
|
|
|
|
|
480
|
|
|
if (is_null($this->fromConfig)) { |
|
481
|
|
|
$this->fromConfig = include 'CalendarSettings/' . |
|
482
|
|
|
ucfirst($this->translate_from) . '.php'; |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
|
|
486
|
|
|
if (is_null($this->toConfig)) { |
|
487
|
|
|
$this->toConfig = include 'CalendarSettings/' . |
|
488
|
|
|
ucfirst($this->translate_to) . '.php'; |
|
489
|
|
|
} |
|
490
|
|
|
|
|
491
|
|
|
$string_date = $this->date_time->format($format); |
|
492
|
|
|
|
|
493
|
|
|
if ($this->translate_to != 'gregorian') { |
|
494
|
|
|
$string_date = str_replace( |
|
495
|
|
|
$this->fromConfig[ 'month' ], |
|
496
|
|
|
$this->toConfig[ 'month' ], |
|
497
|
|
|
$string_date |
|
498
|
|
|
); |
|
499
|
|
|
|
|
500
|
|
|
$string_date = str_replace( |
|
501
|
|
|
$this->fromConfig[ 'days_of_week' ], |
|
502
|
|
|
$this->toConfig[ 'days_of_week' ][ $this->gregorian_DayofWeek ], |
|
503
|
|
|
$string_date |
|
504
|
|
|
); |
|
505
|
|
|
|
|
506
|
|
|
$string_date = str_replace( |
|
507
|
|
|
$this->fromConfig[ 'numbers' ], |
|
508
|
|
|
$this->toConfig[ 'numbers' ], |
|
509
|
|
|
$string_date |
|
510
|
|
|
); |
|
511
|
|
|
|
|
512
|
|
|
$string_date = str_replace( |
|
513
|
|
|
$this->fromConfig[ 'am_time' ], |
|
514
|
|
|
$this->toConfig[ 'am_time' ], |
|
515
|
|
|
$string_date |
|
516
|
|
|
); |
|
517
|
|
|
|
|
518
|
|
|
$string_date = str_replace( |
|
519
|
|
|
$this->fromConfig[ 'pm_time' ], |
|
520
|
|
|
$this->toConfig[ 'pm_time' ], |
|
521
|
|
|
$string_date |
|
522
|
|
|
); |
|
523
|
|
|
|
|
524
|
|
|
$string_date = str_replace( |
|
525
|
|
|
$this->fromConfig[ 'end_of_days' ], |
|
526
|
|
|
$this->toConfig[ 'end_of_days' ], |
|
527
|
|
|
$string_date |
|
528
|
|
|
); |
|
529
|
|
|
} |
|
530
|
|
|
|
|
531
|
|
|
// foreach( $this->translate_to_file as $key => $value ) { |
|
|
|
|
|
|
532
|
|
|
// |
|
533
|
|
|
// $string_date = str_replace( |
|
534
|
|
|
// $this->translate_from_file[ $key ], |
|
|
|
|
|
|
535
|
|
|
// $this->translate_to_file[ $key ], |
|
|
|
|
|
|
536
|
|
|
// $string_date |
|
537
|
|
|
// ); |
|
538
|
|
|
// |
|
539
|
|
|
// } |
|
540
|
|
|
|
|
541
|
|
|
return $string_date; |
|
542
|
|
|
|
|
543
|
|
|
} |
|
544
|
|
|
} |
|
545
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.