1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* RelativeTime.php |
4
|
|
|
* |
5
|
|
|
* @author Michael Pratt <[email protected]> |
6
|
|
|
* @link http://www.michael-pratt.com/ |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace RelativeTime; |
14
|
|
|
|
15
|
|
|
use \RelativeTime\Languages\English; |
16
|
|
|
use \RelativeTime\Translation; |
17
|
|
|
use \DateTime; |
18
|
|
|
use \DateInterval; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The Main Class of the library |
22
|
|
|
*/ |
23
|
|
|
class RelativeTime |
24
|
|
|
{ |
25
|
|
|
/** @var int Class constant with the current Version of this library */ |
26
|
|
|
const VERSION = '1.5.4'; |
27
|
|
|
|
28
|
|
|
/** @var array Array With configuration options **/ |
29
|
|
|
protected $config = array(); |
30
|
|
|
|
31
|
|
|
/** @var object instance of \Relativetime\Translation **/ |
32
|
|
|
protected $translation; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Construct |
36
|
|
|
* |
37
|
|
|
* @param array $config Associative array with configuration directives |
38
|
|
|
* |
39
|
|
|
*/ |
40
|
16 |
|
public function __construct(array $config = array()) |
41
|
|
|
{ |
42
|
16 |
|
$this->config = array_merge(array( |
43
|
16 |
|
'language' => new English(), |
44
|
16 |
|
'separator' => ', ', |
45
|
16 |
|
'suffix' => true, |
46
|
16 |
|
'truncate' => 0, |
47
|
16 |
|
), $config); |
48
|
|
|
|
49
|
16 |
|
$this->translation = new Translation($this->config); |
50
|
16 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Converts 2 dates to its relative time. |
54
|
|
|
* |
55
|
|
|
* @param string $fromTime |
56
|
|
|
* @param string $toTime When null is given, uses the current date. |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
16 |
|
public function convert($fromTime, $toTime = null) |
60
|
|
|
{ |
61
|
16 |
|
$interval = $this->getInterval($fromTime, $toTime); |
62
|
14 |
|
$units = $this->calculateUnits($interval); |
63
|
|
|
|
64
|
14 |
|
return $this->translation->translate($units, $interval->invert); |
65
|
12 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Tells the time passed between the current date and the given date |
69
|
|
|
* |
70
|
|
|
* @param string $date |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
10 |
|
public function timeAgo($date) |
74
|
|
|
{ |
75
|
10 |
|
$interval = $this->getInterval(time(), $date); |
76
|
2 |
|
if ($interval->invert) { |
77
|
8 |
|
return $this->convert(time(), $date); |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
return $this->translation->translate(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Tells the time until the given date |
85
|
|
|
* |
86
|
|
|
* @param string $date |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
2 |
|
public function timeLeft($date) |
90
|
|
|
{ |
91
|
2 |
|
$interval = $this->getInterval($date, time()); |
92
|
2 |
|
if ($interval->invert) { |
93
|
2 |
|
return $this->convert(time(), $date); |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
return $this->translation->translate(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Calculates the interval between the dates and returns |
101
|
|
|
* an array with the valid time. |
102
|
|
|
* |
103
|
|
|
* @param string $fromTime |
104
|
|
|
* @param string $toTime When null is given, uses the current date. |
105
|
|
|
* @return DateInterval |
106
|
|
|
*/ |
107
|
16 |
|
protected function getInterval($fromTime, $toTime = null) |
108
|
|
|
{ |
109
|
16 |
|
$fromTime = new DateTime($this->normalizeDate($fromTime)); |
110
|
14 |
|
$toTime = new DateTime($this->normalizeDate($toTime)); |
111
|
|
|
|
112
|
14 |
|
return $fromTime->diff($toTime); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Normalizes a date for the DateTime class |
117
|
|
|
* |
118
|
|
|
* @param string $date |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
16 |
|
protected function normalizeDate($date) |
122
|
|
|
{ |
123
|
16 |
|
$date = str_replace(array('/', '|'), '-', $date); |
124
|
|
|
|
125
|
16 |
|
if (empty($date)) { |
126
|
2 |
|
return date('Y-m-d H:i:s'); |
127
|
16 |
|
} else if (ctype_digit($date)) { |
128
|
8 |
|
return date('Y-m-d H:i:s', $date); |
129
|
|
|
} |
130
|
|
|
|
131
|
10 |
|
return $date; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Given a DateInterval, creates an array with the time |
136
|
|
|
* units and truncates it when necesary. |
137
|
|
|
* |
138
|
|
|
* @param DateInterval $interval |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
14 |
|
protected function calculateUnits(DateInterval $interval) |
142
|
|
|
{ |
143
|
14 |
|
$units = array_filter(array( |
144
|
14 |
|
'years' => (int) $interval->y, |
145
|
14 |
|
'months' => (int) $interval->m, |
146
|
14 |
|
'days' => (int) $interval->d, |
147
|
14 |
|
'hours' => (int) $interval->h, |
148
|
14 |
|
'minutes' => (int) $interval->i, |
149
|
14 |
|
'seconds' => (int) $interval->s, |
150
|
14 |
|
)); |
151
|
|
|
|
152
|
14 |
|
if (empty($units)) { |
153
|
4 |
|
return array(); |
154
|
12 |
|
} else if ((int) $this->config['truncate'] > 0) { |
155
|
2 |
|
return array_slice($units, 0, (int) $this->config['truncate']); |
156
|
|
|
} |
157
|
|
|
|
158
|
12 |
|
return $units; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
?> |
|
|
|
|
162
|
|
|
|
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.