forked from jimmiw/php-time-ago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwestsworld.datetime.class.php
231 lines (205 loc) · 5.5 KB
/
westsworld.datetime.class.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
<?php
/**
* NOTE: This class is no longer maintained as of version 0.2.0.
* You should use the timeAgoInWords("date") convenience method instead.
*
* A specialization of the DateTime class, that can print out,
* "how long ago" it was.
*
* @author jimmiw
* @since 2009-09-28
* @site http://github.com/jimmiw/php-time-ago
*/
class WWDateTime extends DateTime {
/**
*
*/
public function __construct(/*string*/ $time = "now",
DateTimeZone $timezone = NULL) {
// 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 = new DateTimeZone('Europe/London');
}
// initializes the parant
parent::__construct($time, $timezone);
}
/**
* Returns how long time ago this date object is, in words.
* E.g. current date could be "2009-01-02" and this object could hold
* "2009-01-01", then this function would return "1 day".
*
* @return the number of time ago, in words (e.g. 2 days).
*/
public function timeAgoInWords() {
$timeAgoInWords = NULL;
$yearDiff = 0;
$monthDiff = 0;
$dayDiff = 0;
$hourDiff = 0;
$minuteDiff = 0;
$secondDiff = 0;
$now = new DateTime("now", $this->getTimezone());
// tests the years
$yearDiff = $this->findDiff($now, $this, 'Y');
if($yearDiff == 1) {
$timeAgoInWords = $this->constructTimeAgoWord(
$yearDiff,
'year',
1,
'about'
);
}
else if($yearDiff >= 2) {
$timeAgoInWords = $this->constructTimeAgoWord(
$yearDiff,
'year',
1,
'over'
);
}
// tests the months
if($timeAgoInWords == NULL) {
$timeAgoInWords = $this->constructTimeAgoWord(
$this->findDiff($now, $this, 'n'),
'month',
1,
'about'
);
}
// tests the days
if($timeAgoInWords == NULL) {
$timeAgoInWords = $this->constructTimeAgoWord(
$this->findDiff($now, $this, 'j'),
'day',
1
);
}
// tests the hours
if($timeAgoInWords == NULL) {
$timeAgoInWords = $this->constructTimeAgoWord(
$this->findDiff($now, $this, 'G'),
'hour',
1,
'about'
);
}
// tests the minutes
if($timeAgoInWords == NULL) {
$minuteDiff = $this->findDiff($now, $this, 'i');
// if under 44 mins!
if($minuteDiff <= 44) {
$timeAgoInWords = $this->constructTimeAgoWord(
$minuteDiff,
'minute',
1
);
}
// else it's about an hour
else {
$timeAgoInWords = $this->constructTimeAgoWord(
1,
'hour',
1,
'about'
);
}
}
// tests the seconds
if($timeAgoInWords == NULL) {
$secondDiff = $this->findDiff($now, $this, 's');
// if under 29 secs
if($secondDiff <= 29) {
$timeAgoInWords = "less than a minute";
}
// else it's a minute!
else {
$timeAgoInWords = $this->constructTimeAgoWord(
1,
'minute'
);
}
}
return $timeAgoInWords;
}
/**
* Finds the difference in the two DateTime objects, using the given format.
* @param from
* @param to
* @param format
* @return the difference in the two DateTime objects
*/
private function findDiff(DateTime $from = NULL,
DateTime $to = NULL,
$format = NULL) {
return $from->format($format) - $to->format($format);
}
/**
* Constructs the actual "time ago"-word
* @param timeDifference
* @param timeName
* @param decidingTimeDifference
* @param prefix
* @param postfix
* @return the "time ago"-word generated
*/
private function constructTimeAgoWord($timeDiffrence = 0,
$timeName = NULL,
$decidingTimeDifference = 1,
$prefix = NULL,
$postfix = NULL) {
// initializes the timeAgoInWord placeholder
$timeAgoInWords = NULL;
if($timeDiffrence > 0) {
// sets the difference
$timeAgoInWords = $timeDiffrence . " ";
// adds the "prefix word", if any
if($prefix != NULL) {
// blindly adds a space between the words
$timeAgoInWords = $prefix . " " . $timeAgoInWords;
}
// tests if we are to pluralize the time name or not
if($timeDiffrence > $decidingTimeDifference) {
$timeAgoInWords .= $this->pluralize($timeName);
}
else {
$timeAgoInWords .= $timeName;
}
// adds the "postfix word", if any
if($postfix != NULL) {
// blindly adds a space between the words
$timeAgoInWords .= " " . $postfix;
}
}
// returns the "time ago in words" found, else NULL
return $timeAgoInWords;
}
/**
* Pluralizes the given word (only if it's in my list ofc!)
* @param $word the word to pluralize
* @return the pluralized word, if possible.
*/
private function pluralize($word = NULL) {
$pluralizedWord = $word;
if($word == 'year') {
$pluralizedWord = 'years';
}
else if($word == 'month') {
$pluralizedWord = 'months';
}
else if($word == 'day') {
$pluralizedWord = 'days';
}
else if($word == 'hour') {
$pluralizedWord = 'hours';
}
else if($word == 'minute') {
$pluralizedWord = 'minutes';
}
else if($word == 'second') {
$pluralizedWord = 'seconds';
}
return $pluralizedWord;
}
}
?>