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