/
HumanDiff.php
279 lines (254 loc) · 7.68 KB
/
HumanDiff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* Part of Date_HumanDiff
*
* PHP version 5
*
* @category Date
* @package Date_HumanDiff
* @author Christian Weiske <cweiske@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* @link http://pear.php.net/package/Date_HumanDiff
*/
/**
* Generate textual time differences that are easily understandable by humans.
*
* The class supports minutes, hours, days, weeks, months and years.
*
* Examples:
* - 5 seconds ago -> "just now"
* - 65 seconds ago -> "a minute ago"
* - 120 seconds ago -> "2 minutes ago"
*
* @category Date
* @package Date_HumanDiff
* @author Christian Weiske <cweiske@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* @version Release: @package_version@
* @link http://pear.php.net/package/Date_HumanDiff
*/
class Date_HumanDiff
{
static $MINUTE = 60;
static $HOUR = 3600;//static::$MINUTE * 60
static $DAY = 86400;//static::$HOUR * 24
static $WEEK = 604800;//static::$DAY * 7
static $MONTH = 2628000;//static::$YEAR / 12
static $YEAR = 31536000;//static::$DAY * 365
/**
* Array of possible time difference display formats.
* Each value is an array with the following values:
* - max. time difference
* - textual description
* - number to divide the time difference by
*
* @var array
* @usedby get()
*/
protected $formats;
/**
* Translation object.
* Provides translations for time strings
*
* @var Date_HumanDiff_Lang
*/
protected $translator;
/**
* Create new instance, initialize $formats array
*/
public function __construct()
{
$this->formats = array(
array(0.7 * static::$MINUTE, 'just now', 1),
array(1.5 * static::$MINUTE, 'a minute ago', 1),
array( 60 * static::$MINUTE, '%d minutes ago', static::$MINUTE),
array(1.5 * static::$HOUR, 'an hour ago', 1),
array( static::$DAY, '%d hours ago', static::$HOUR),
array( 2 * static::$DAY, 'yesterday', 1),
array( 7 * static::$DAY, '%d days ago', static::$DAY),
array(1.5 * static::$WEEK, 'a week ago', 1),
array( static::$MONTH, '%d weeks ago', static::$WEEK),
array(1.5 * static::$MONTH, 'a month ago', 1),
array( static::$YEAR, '%d months ago', static::$MONTH),
array(1.5 * static::$YEAR, 'a year ago', 1),
array(PHP_INT_MAX, '%d years ago', static::$YEAR)
);
}
/**
* Generate a human readable time difference.
*
* @param int|DateTime $timestamp Timestamp to get difference to
* @param int|DateTime $reference Reference timestamp to get difference from.
* If omitted, it's set to the current time.
*
* @return string Human readable time difference ("a week ago")
*/
public function get($timestamp, $reference = null)
{
if ($reference === null) {
$reference = time();
}
$timestamp = $this->makeTimestamp($timestamp);
$reference = $this->makeTimestamp($reference);
$delta = $reference - $timestamp;
foreach ($this->formats as $format) {
if ($delta < $format[0]) {
return sprintf(
$this->getTranslation($format[1]),
round($delta / $format[2])
);
}
};
}
/**
* Adds a new format to the list of time formats.
*
* @param integer $timeDiff Maximum time difference between reference time
* and given time in seconds.
* @param string $text Text to show when the format is used
* @param integer $divider Number to divide time difference by
* @param boolean $sort Sort formats. Don't use this if you add many formats
* but call sortFormats() manually afterwards.
*
* @return void
*/
public function addFormat($timeDiff, $text, $divider, $sort = true)
{
$this->formats[] = array($timeDiff, $text, $divider);
if ($sort) {
$this->sortFormats();
}
}
/**
* Delete all existing formats.
*
* @return void
*/
public function clearFormats()
{
$this->formats = array();
}
/**
* Sort all formats by their time difference
*
* @return void
*/
public function sortFormats()
{
usort($this->formats, array($this, 'sortFormatCompare'));
}
/**
* Comparision function for usort()
*
* @param array $a First comparator
* @param array $b Second comparator
*
* @return integer <, = or > 0 - when $a is less, equal or greater than $b
*/
public function sortFormatCompare($a, $b)
{
return $a[0] - $b[0];
}
/**
* Convert given variable to a unix timestamp.
*
* Supported formats:
* - DateTime object
* - unix timestamps
* - Strings that can be converted with strtotime
*
* @param mixed $something Some variable
*
* @return integer Unix timestamp
*/
protected function makeTimestamp($something)
{
if ($something instanceof DateTime) {
return $something->getTimestamp();
}
if (is_numeric($something)) {
return (int)$something;
}
return strtotime($something);
}
/**
* Get the translation for the given string.
*
* @param string $string String to translate
*
* @return string Translated string. Original string when no translation
* exists.
*/
protected function getTranslation($string)
{
if ($this->translator === null) {
return $string;
}
return $this->translator->get($string);
}
/**
* Set the object that's used to translate time strings
*
* @param object $translator Language translation object
*
* @return void
*/
public function setTranslator(Date_HumanDiff_Locale $translator)
{
$this->translator = $translator;
}
/**
* Set the locale to use.
*
* Supported formats:
* - 2-letter ISO code ("de", "fr")
* - locale name with and without encoding ("de_AT", "fr_FR.utf8")
*
* @param string $lang Language name
*
* @return boolean True if the translations could be loaded, false if not.
*/
public function setLocale($lang)
{
if (strlen($lang) > 2) {
//split off encoding
list($locale,) = explode('.', $lang);
if (!preg_match('#^[a-z_]+$#i', $locale)) {
return false;
}
$lang = $locale;
}
$class = 'Date_HumanDiff_Locale_' . $lang;
$file = str_replace('_', '/', $class) . '.php';
if ($this->isIncludable($file)) {
include_once $file;
}
if (!class_exists($class)) {
if (strlen($lang) > 2) {
//try main language without country
return $this->setLocale(substr($lang, 0, 2));
}
return false;
}
$translator = new $class();
$this->setTranslator($translator);
return true;
}
/**
* Check if the given file is includable
*
* @param string $file Path to file (relative to include path)
*
* @return boolean True if one can include() it
*/
protected function isIncludable($file)
{
$hdl = @fopen($file, 'r', true);
if ($hdl === false) {
return false;
}
fclose($hdl);
return true;
}
}
?>