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( |
205
|
|
|
$year = 2000, |
206
|
|
|
$month = 1, |
207
|
|
|
$day = 1, |
208
|
|
|
$hour = 0, |
209
|
|
|
$minute = 0, |
210
|
|
|
$second = 0 |
211
|
|
|
) { |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* When we want to set a Datetime object to Datium |
216
|
|
|
*/ |
217
|
|
|
if (func_num_args() === 1) { |
218
|
|
|
self::$static_date_time = func_get_arg(0); |
219
|
|
|
|
220
|
|
|
self::$call_type = 'set'; |
221
|
|
|
} else { |
222
|
|
|
self::$array_date = array( |
223
|
|
|
'year' => $year, |
224
|
|
|
'month' => $month, |
225
|
|
|
'day' => $day, |
226
|
|
|
'hour' => $hour, |
227
|
|
|
'minute' => $minute, |
228
|
|
|
'second' => $second |
229
|
|
|
); |
230
|
|
|
|
231
|
|
|
self::$call_type = 'make'; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return new Datium(); |
235
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Select The range between two date object |
240
|
|
|
* |
241
|
|
|
* @param object $date_start Start of the DateTime |
242
|
|
|
* @param object $date_end End of the DateTime |
243
|
|
|
* |
244
|
|
|
* @return object |
245
|
|
|
*/ |
246
|
|
|
public static function between($date_start, $date_end) |
247
|
|
|
{ |
248
|
|
|
|
249
|
|
|
self::$date_start = $date_start; |
250
|
|
|
|
251
|
|
|
self::$date_end = $date_end; |
252
|
|
|
|
253
|
|
|
self::$call_type = 'between'; |
254
|
|
|
|
255
|
|
|
return new Datium(); |
256
|
|
|
|
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Convert from current calendar, what type is current calendar? |
261
|
|
|
* |
262
|
|
|
* @param object $calendar Assigned calendar to start from |
263
|
|
|
* |
264
|
|
|
* @return $object |
|
|
|
|
265
|
|
|
*/ |
266
|
|
View Code Duplication |
public function from($calendar) |
|
|
|
|
267
|
|
|
{ |
268
|
|
|
|
269
|
|
|
$this->convert = new Convert($this->date_time); |
|
|
|
|
270
|
|
|
|
271
|
|
|
$this->date_time = $this->convert->from($calendar); |
|
|
|
|
272
|
|
|
|
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* We need this part for DayOf class |
276
|
|
|
*/ |
277
|
|
|
$this->calendar_type = $calendar; |
278
|
|
|
|
279
|
|
|
$this->translate_to = $calendar; |
280
|
|
|
|
281
|
|
|
return $this; |
282
|
|
|
|
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Convert date to current Date |
287
|
|
|
* |
288
|
|
|
* @param object $calendar Assigned calendar to when calendar should start. |
289
|
|
|
* |
290
|
|
|
* @return object |
291
|
|
|
*/ |
292
|
|
View Code Duplication |
public function to($calendar) |
|
|
|
|
293
|
|
|
{ |
294
|
|
|
|
295
|
|
|
$this->convert = new Convert($this->date_time); |
|
|
|
|
296
|
|
|
|
297
|
|
|
$this->date_time = $this->convert->to($calendar); |
|
|
|
|
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* We need this part for DayOf class |
301
|
|
|
*/ |
302
|
|
|
$this->calendar_type = $calendar; |
303
|
|
|
|
304
|
|
|
$this->translate_to = $calendar; |
305
|
|
|
|
306
|
|
|
return $this; |
307
|
|
|
|
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Difference between two time |
313
|
|
|
* |
314
|
|
|
* @param DateTime $start Start of the date |
315
|
|
|
* @param DateTime $end End of the date |
316
|
|
|
* |
317
|
|
|
* @return object |
318
|
|
|
*/ |
319
|
|
|
public static function diff($start, $end) |
320
|
|
|
{ |
321
|
|
|
|
322
|
|
|
return date_diff($start, $end); |
323
|
|
|
|
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Add new date value to current date |
328
|
|
|
* |
329
|
|
|
* @param string $value How much date should be added to current date |
330
|
|
|
* |
331
|
|
|
* @return object |
332
|
|
|
*/ |
333
|
|
View Code Duplication |
public function add($value) |
|
|
|
|
334
|
|
|
{ |
335
|
|
|
|
336
|
|
|
$this->date_interval_expression = str_replace( |
337
|
|
|
$this->config[ 'date_simple' ], |
338
|
|
|
$this->config[ 'date_interval' ], |
339
|
|
|
$value |
340
|
|
|
); |
341
|
|
|
|
342
|
|
|
$this->date_interval_expression = str_replace( |
343
|
|
|
' ', |
344
|
|
|
'', |
345
|
|
|
'P' . $this->date_interval_expression |
346
|
|
|
); |
347
|
|
|
|
348
|
|
|
$this->date_time->add( |
349
|
|
|
new DateInterval($this->date_interval_expression) |
350
|
|
|
); |
351
|
|
|
|
352
|
|
|
return $this; |
353
|
|
|
|
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Sub date from current date |
358
|
|
|
* |
359
|
|
|
* @param string $value How much date should increase from current date |
360
|
|
|
* |
361
|
|
|
* @return obejct |
362
|
|
|
*/ |
363
|
|
View Code Duplication |
public function sub($value) |
|
|
|
|
364
|
|
|
{ |
365
|
|
|
|
366
|
|
|
$this->date_interval_expression = str_replace( |
367
|
|
|
$this->config[ 'date_simple' ], |
368
|
|
|
$this->config[ 'date_interval' ], |
369
|
|
|
$value |
370
|
|
|
); |
371
|
|
|
|
372
|
|
|
$this->date_interval_expression = str_replace( |
373
|
|
|
' ', |
374
|
|
|
'', |
375
|
|
|
'P' . $this->date_interval_expression |
376
|
|
|
); |
377
|
|
|
|
378
|
|
|
$this->date_time->sub( |
379
|
|
|
new DateInterval($this->date_interval_expression) |
380
|
|
|
); |
381
|
|
|
|
382
|
|
|
return $this; |
383
|
|
|
|
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Check if current year is leap or not |
388
|
|
|
* |
389
|
|
|
* @param string $type Name of the calendar to caculate leap year |
|
|
|
|
390
|
|
|
* |
391
|
|
|
* @return boolean |
392
|
|
|
*/ |
393
|
|
|
public function leap() |
394
|
|
|
{ |
395
|
|
|
|
396
|
|
|
$this->leap = new Leap($this->date_time->format('Y'), $this->calendar_type); |
|
|
|
|
397
|
|
|
|
398
|
|
|
return $this->leap; |
399
|
|
|
|
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Caculate day of year or week |
404
|
|
|
* |
405
|
|
|
* @since Aug, 22 2015 |
406
|
|
|
* |
407
|
|
|
* @return integer |
408
|
|
|
*/ |
409
|
|
|
public function dayOf() |
410
|
|
|
{ |
411
|
|
|
|
412
|
|
|
$this->day_of = new DayOf($this->date_time, $this->calendar_type); |
|
|
|
|
413
|
|
|
|
414
|
|
|
return $this->day_of; |
415
|
|
|
|
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
// public function events() |
|
|
|
|
419
|
|
|
// { |
420
|
|
|
// |
421
|
|
|
// if (Datium::$call_type == 'between' ) { |
|
|
|
|
422
|
|
|
// |
423
|
|
|
// $this->events = new Events(Datium::$date_start, Datium::$date_end); |
|
|
|
|
424
|
|
|
// |
425
|
|
|
// } else { |
|
|
|
|
426
|
|
|
// |
427
|
|
|
// $this->events = new Events($this->date_time); |
|
|
|
|
428
|
|
|
// |
429
|
|
|
// } |
430
|
|
|
// |
431
|
|
|
// return $this->events; |
|
|
|
|
432
|
|
|
// |
433
|
|
|
// } |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Return Datetime as a original object |
437
|
|
|
* |
438
|
|
|
* @since Oct 22, 2015 |
439
|
|
|
* |
440
|
|
|
* @return object |
441
|
|
|
*/ |
442
|
|
|
public function object() |
443
|
|
|
{ |
444
|
|
|
|
445
|
|
|
return $this->date_time; |
446
|
|
|
|
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* Translate current date string to selected language |
451
|
|
|
* |
452
|
|
|
* @param string $language language short name fa, en, ar ... |
453
|
|
|
* |
454
|
|
|
* @return object |
455
|
|
|
*/ |
456
|
|
|
public function lang($language = 'fa') |
457
|
|
|
{ |
458
|
|
|
|
459
|
|
|
$this->language = $language; |
460
|
|
|
|
461
|
|
|
$this->result = new Lang(); |
|
|
|
|
462
|
|
|
|
463
|
|
|
$this->result->setConfig($this->language); |
464
|
|
|
|
465
|
|
|
$this->toConfig = $this->result->getConfig(); |
466
|
|
|
|
467
|
|
|
return $this; |
468
|
|
|
|
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* Return object as timestamp |
473
|
|
|
* |
474
|
|
|
* @return timestamp |
475
|
|
|
*/ |
476
|
|
|
public function timestamp() |
477
|
|
|
{ |
478
|
|
|
|
479
|
|
|
return strtotime($this->date_time->format('Y-m-d H:i:s')); |
480
|
|
|
|
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Return fainal result |
485
|
|
|
* |
486
|
|
|
* @param string $format Date format |
487
|
|
|
* |
488
|
|
|
* @since Aug 17 2015 |
489
|
|
|
* |
490
|
|
|
* @return string |
491
|
|
|
*/ |
492
|
|
|
public function get($format = 'Y-m-d H:i:s') |
493
|
|
|
{ |
494
|
|
|
|
495
|
|
|
// $this->translate_from_file = include( 'Lang/en/general.php' ); |
|
|
|
|
496
|
|
|
// |
497
|
|
|
// $this->translate_to_file = include( 'Lang/' . $this->language . '/general.php' ); |
|
|
|
|
498
|
|
|
|
499
|
|
|
if (is_null($this->fromConfig)) { |
500
|
|
|
$this->fromConfig = include 'CalendarSettings/' . |
501
|
|
|
ucfirst($this->translate_from) . '.php'; |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
|
505
|
|
|
if (is_null($this->toConfig)) { |
506
|
|
|
$this->toConfig = include 'CalendarSettings/' . |
507
|
|
|
ucfirst($this->translate_to) . '.php'; |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
$string_date = $this->date_time->format($format); |
511
|
|
|
|
512
|
|
|
if ($this->translate_to != 'gregorian') { |
513
|
|
|
$string_date = str_replace( |
514
|
|
|
$this->fromConfig[ 'month' ], |
515
|
|
|
$this->toConfig[ 'month' ], |
516
|
|
|
$string_date |
517
|
|
|
); |
518
|
|
|
|
519
|
|
|
$string_date = str_replace( |
520
|
|
|
$this->fromConfig[ 'days_of_week' ], |
521
|
|
|
$this->toConfig[ 'days_of_week' ][ $this->gregorian_DayofWeek ], |
522
|
|
|
$string_date |
523
|
|
|
); |
524
|
|
|
|
525
|
|
|
$string_date = str_replace( |
526
|
|
|
$this->fromConfig[ 'numbers' ], |
527
|
|
|
$this->toConfig[ 'numbers' ], |
528
|
|
|
$string_date |
529
|
|
|
); |
530
|
|
|
|
531
|
|
|
$string_date = str_replace( |
532
|
|
|
$this->fromConfig[ 'am_time' ], |
533
|
|
|
$this->toConfig[ 'am_time' ], |
534
|
|
|
$string_date |
535
|
|
|
); |
536
|
|
|
|
537
|
|
|
$string_date = str_replace( |
538
|
|
|
$this->fromConfig[ 'pm_time' ], |
539
|
|
|
$this->toConfig[ 'pm_time' ], |
540
|
|
|
$string_date |
541
|
|
|
); |
542
|
|
|
|
543
|
|
|
$string_date = str_replace( |
544
|
|
|
$this->fromConfig[ 'end_of_days' ], |
545
|
|
|
$this->toConfig[ 'end_of_days' ], |
546
|
|
|
$string_date |
547
|
|
|
); |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
// foreach( $this->translate_to_file as $key => $value ) { |
|
|
|
|
551
|
|
|
// |
552
|
|
|
// $string_date = str_replace( |
553
|
|
|
// $this->translate_from_file[ $key ], |
|
|
|
|
554
|
|
|
// $this->translate_to_file[ $key ], |
|
|
|
|
555
|
|
|
// $string_date |
556
|
|
|
// ); |
557
|
|
|
// |
558
|
|
|
// } |
559
|
|
|
|
560
|
|
|
return $string_date; |
561
|
|
|
|
562
|
|
|
} |
563
|
|
|
} |
564
|
|
|
|
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.