1
|
|
|
<?php |
2
|
|
|
namespace morphos; |
3
|
|
|
|
4
|
|
|
use DateInterval; |
5
|
|
|
use DateTime; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
abstract class TimeSpeller |
10
|
|
|
{ |
11
|
|
|
const YEAR = 'year'; |
12
|
|
|
const MONTH = 'month'; |
13
|
|
|
const DAY = 'day'; |
14
|
|
|
const HOUR = 'hour'; |
15
|
|
|
const MINUTE = 'minute'; |
16
|
|
|
const SECOND = 'second'; |
17
|
|
|
|
18
|
|
|
const AGO = 'ago'; |
19
|
|
|
const IN = 'in'; |
20
|
|
|
|
21
|
|
|
const AND_WORD = 'and'; |
22
|
|
|
|
23
|
|
|
const JUST_NOW = 'just now'; |
24
|
|
|
|
25
|
|
|
const DIRECTION = 1; |
26
|
|
|
const SEPARATE = 2; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @abstract |
30
|
|
|
* @param int $count |
31
|
|
|
* @param string $unit |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public static function spellUnit($count, $unit) { |
35
|
|
|
throw new RuntimeException('Not implemented'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param DateInterval $interval |
40
|
|
|
* @param int $options |
41
|
|
|
* @param int $limit |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
22 |
|
public static function spellInterval(DateInterval $interval, $options = 0, $limit = 0) |
45
|
|
|
{ |
46
|
22 |
|
$parts = []; |
47
|
22 |
|
$k = 0; |
48
|
|
|
foreach ([ |
49
|
22 |
|
'y' => static::YEAR, |
50
|
22 |
|
'm' => static::MONTH, |
51
|
22 |
|
'd' => static::DAY, |
52
|
22 |
|
'h' => static::HOUR, |
53
|
22 |
|
'i' => static::MINUTE, |
54
|
22 |
|
's' => static::SECOND |
55
|
|
|
] as $interval_field => $unit) { |
56
|
22 |
|
if ($interval->{$interval_field} > 0) { |
57
|
22 |
|
if($limit > 0 && $k >= $limit) { |
58
|
6 |
|
break; |
59
|
|
|
} |
60
|
22 |
|
$parts[] = static::spellUnit($interval->{$interval_field}, $unit); |
61
|
22 |
|
$k++; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
22 |
|
if (empty($parts)) { |
66
|
|
|
return static::JUST_NOW; |
67
|
|
|
} |
68
|
|
|
|
69
|
22 |
|
if ($options & static::SEPARATE && count($parts) > 1) { |
70
|
6 |
|
$last_part = array_pop($parts); |
71
|
6 |
|
$spelled = implode(', ', $parts).' '.static::AND_WORD.' '.$last_part; |
72
|
|
|
} else { |
73
|
16 |
|
$spelled = implode(' ', $parts); |
74
|
|
|
} |
75
|
|
|
|
76
|
22 |
|
if ($options & static::DIRECTION) { |
77
|
4 |
|
if ($interval->invert) { |
78
|
|
|
$spelled = static::IN.' '.$spelled; |
79
|
|
|
} else { |
80
|
4 |
|
$spelled .= ' '.static::AGO; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
22 |
|
return $spelled; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param int|string|DateTime $dateTime Unix timestamp |
89
|
|
|
* or datetime in strtotime() format |
90
|
|
|
* or DateTime instance |
91
|
|
|
* |
92
|
|
|
* @param int $options |
93
|
|
|
* @param int $limit |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
* @throws \Exception |
97
|
|
|
*/ |
98
|
6 |
|
public static function spellDifference($dateTime, $options = 0, $limit = 0) |
99
|
|
|
{ |
100
|
6 |
|
$now = new DateTime(); |
101
|
6 |
|
if (is_numeric($dateTime) || is_string($dateTime)) { |
102
|
6 |
|
$dateTime = new DateTime(is_numeric($dateTime) |
103
|
4 |
|
? '@' . $dateTime |
104
|
6 |
|
: $dateTime); |
105
|
|
|
} else if(!($dateTime instanceof DateTime)) { |
106
|
|
|
throw new InvalidArgumentException('dateTime argument should be unix timestamp (int) or date time (string) or DateTime instance'); |
107
|
|
|
} |
108
|
|
|
|
109
|
6 |
|
return static::spellInterval($dateTime->diff($now), $options, $limit); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|