forked from jimmiw/php-time-ago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeago.inc.php
266 lines (241 loc) · 8 KB
/
timeago.inc.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
<?php
function timeAgoInWords($timestring, $timezone = NULL) {
$timeAgo = new TimeAgo($timezone);
return $timeAgo->inWords($timestring, "now");
}
/**
* This class can help you find out just how much time has passed between
* two dates.
*
* It has two functions you can call:
* inWords() which gives you the "time ago in words" between two dates.
* dateDifference() which returns an array of years,months,days,hours,minutes and
* seconds between the two dates.
*
* @author jimmiw
* @since 0.2.0 (2010/05/05)
* @site http://github.com/jimmiw/php-time-ago
*/
class TimeAgo {
// defines the number of seconds per "unit"
private $secondsPerMinute;
private $secondsPerHour;
private $secondsPerDay;
private $secondsPerMonth;
private $secondsPerYear;
private $timezone;
public function __construct($timezone = NULL) {
$this->secondsPerMinute = 60;
$this->secondsPerHour = $this->secondsPerMinute * 60;
$this->secondsPerDay = $this->secondsPerHour * 24;
$this->secondsPerMonth = $this->secondsPerDay * 30;
$this->secondsPerYear = $this->secondsPerMonth * 12;
// if the $timezone is null, we take 'Europe/London' as the default
// this was done, because the parent construct tossed an exception
if($timezone == NULL) {
$timezone = 'Europe/Copenhagen';
}
$this->timezone = $timezone;
}
public function inWords($past, $now = "now") {
// sets the default timezone
date_default_timezone_set($this->timezone);
// finds the past in datetime
$past = strtotime($past);
// finds the current datetime
$now = strtotime($now);
// creates the "time ago" string. This always starts with an "about..."
$timeAgo = "";
// finds the time difference
$timeDifference = $now - $past;
// less than 29secs
if($timeDifference <= 29) {
$timeAgo = "less than a minute";
}
// more than 29secs and less than 1min29secss
else if($timeDifference > 29 && $timeDifference <= 89) {
$timeAgo = "1 minute";
}
// between 1min30secs and 44mins29secs
else if($timeDifference > 89 &&
$timeDifference <= (($this->secondsPerMinute * 44) + 29)
) {
$minutes = floor($timeDifference / $this->secondsPerMinute);
$timeAgo = $minutes." minutes";
}
// between 44mins30secs and 1hour29mins29secs
else if(
$timeDifference > (($this->secondsPerMinute * 44) + 29)
&&
$timeDifference < (($this->secondsPerMinute * 89) + 29)
) {
$timeAgo = "about 1 hour";
}
// between 1hour29mins30secs and 23hours59mins29secs
else if(
$timeDifference > (
($this->secondsPerMinute * 89) +
29
)
&&
$timeDifference <= (
($this->secondsPerHour * 23) +
($this->secondsPerMinute * 59) +
29
)
) {
$hours = floor($timeDifference / $this->secondsPerHour);
$timeAgo = $hours." hours";
}
// between 23hours59mins30secs and 47hours59mins29secs
else if(
$timeDifference > (
($this->secondsPerHour * 23) +
($this->secondsPerMinute * 59) +
29
)
&&
$timeDifference <= (
($this->secondsPerHour * 47) +
($this->secondsPerMinute * 59) +
29
)
) {
$timeAgo = "1 day";
}
// between 47hours59mins30secs and 29days23hours59mins29secs
else if(
$timeDifference > (
($this->secondsPerHour * 47) +
($this->secondsPerMinute * 59) +
29
)
&&
$timeDifference <= (
($this->secondsPerDay * 29) +
($this->secondsPerHour * 23) +
($this->secondsPerMinute * 59) +
29
)
) {
$days = floor($timeDifference / $this->secondsPerDay);
$timeAgo = $days." days";
}
// between 29days23hours59mins30secs and 59days23hours59mins29secs
else if(
$timeDifference > (
($this->secondsPerDay * 29) +
($this->secondsPerHour * 23) +
($this->secondsPerMinute * 59) +
29
)
&&
$timeDifference <= (
($this->secondsPerDay * 59) +
($this->secondsPerHour * 23) +
($this->secondsPerMinute * 59) +
29
)
) {
$timeAgo = "about 1 month";
}
// between 59days23hours59mins30secs and 1year (minus 1sec)
else if(
$timeDifference > (
($this->secondsPerDay * 59) +
($this->secondsPerHour * 23) +
($this->secondsPerMinute * 59) +
29
)
&&
$timeDifference < $this->secondsPerYear
) {
$months = round($timeDifference / $this->secondsPerMonth);
// if months is 1, then set it to 2, because we are "past" 1 month
if($months == 1) {
$months = 2;
}
$timeAgo = $months." months";
}
// between 1year and 2years (minus 1sec)
else if(
$timeDifference >= $this->secondsPerYear
&&
$timeDifference < ($this->secondsPerYear * 2)
) {
$timeAgo = "about 1 year";
}
// 2years or more
else {
$years = floor($timeDifference / $this->secondsPerYear);
$timeAgo = "over ".$years." years";
}
return $timeAgo;
}
public function dateDifference($past, $now = "now") {
// initializes the placeholders for the different "times"
$seconds = 0;
$minutes = 0;
$hours = 0;
$days = 0;
$months = 0;
$years = 0;
// sets the default timezone
date_default_timezone_set($this->timezone);
// finds the past in datetime
$past = strtotime($past);
// finds the current datetime
$now = strtotime($now);
// calculates the difference
$timeDifference = $now - $past;
// starts determining the time difference
if($timeDifference >= 0) {
switch($timeDifference) {
// finds the number of years
case ($timeDifference >= $this->secondsPerYear):
// uses floor to remove decimals
$years = floor($timeDifference / $this->secondsPerYear);
// saves the amount of seconds left
$timeDifference = $timeDifference-($years * $this->secondsPerYear);
// finds the number of months
case ($timeDifference >= $this->secondsPerMonth && $timeDifference <= ($this->secondsPerYear-1)):
// uses floor to remove decimals
$months = floor($timeDifference / $this->secondsPerMonth);
// saves the amount of seconds left
$timeDifference = $timeDifference-($months * $this->secondsPerMonth);
// finds the number of days
case ($timeDifference >= $this->secondsPerDay && $timeDifference <= ($this->secondsPerYear-1)):
// uses floor to remove decimals
$days = floor($timeDifference / $this->secondsPerDay);
// saves the amount of seconds left
$timeDifference = $timeDifference-($days * $this->secondsPerDay);
// finds the number of hours
case ($timeDifference >= $this->secondsPerHour && $timeDifference <= ($this->secondsPerDay-1)):
// uses floor to remove decimals
$hours = floor($timeDifference / $this->secondsPerHour);
// saves the amount of seconds left
$timeDifference = $timeDifference-($hours * $this->secondsPerHour);
// finds the number of minutes
case ($timeDifference >= $this->secondsPerMinute && $timeDifference <= ($this->secondsPerHour-1)):
// uses floor to remove decimals
$minutes = floor($timeDifference / $this->secondsPerMinute);
// saves the amount of seconds left
$timeDifference = $timeDifference-($minutes * $this->secondsPerMinute);
// finds the number of seconds
case ($timeDifference <= ($this->secondsPerMinute-1)):
// seconds is just what there is in the timeDifference variable
$seconds = $timeDifference;
}
}
$difference = array(
"years" => $years,
"months" => $months,
"days" => $days,
"hours" => $hours,
"minutes" => $minutes,
"seconds" => $seconds
);
return $difference;
}
}
?>