-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdate.php
More file actions
205 lines (169 loc) · 3.9 KB
/
Copy pathdate.php
File metadata and controls
205 lines (169 loc) · 3.9 KB
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
<?php namespace Feather\Components\Support;
use DateTime;
use Feather\Components\Foundation\Component;
class Date extends Component {
/**
* Array of the fuzzy date periods and lengths.
*
* @var array
*/
protected $fuzzy = array(
'periods' => array('second', 'minute', 'hour', 'day', 'week', 'month', 'year'),
'lengths' => array(60, 60, 24, 7, 4.35, 12)
);
/**
* Alternate text to show if date is invalid.
*
* @var string
*/
public $alternate;
/**
* Text to show before the returned formatted string.
*
* @var string
*/
public $prefix;
/**
* Text to show after the returned formatted string.
*
* @var string
*/
public $suffix;
/**
* The DateTime being used for the current instance.
*
* @var object
*/
public $date;
/**
* Set the date to use.
*
* @param int|string $date
* @return Feather\Components\Support\Date
*/
public function set($date)
{
$this->date = new DateTime;
if(is_numeric($date))
{
$this->date->setTimestamp($date);
}
else
{
$this->date->setTimestamp(strtotime($date));
}
return $this;
}
/**
* Format a date and return it for the discussion meta data.
*
* @param string|int $date
* @return string
*/
public function meta($date)
{
$this->set($date);
return '<span title="' . $this->show('long') . '">' . $this->relative('short') . '</span>';
}
/**
* Sets the alternate text to display if the date is of an invalid format.
*
* @param string $text
* @return Feather\Components\Support\Date
*/
public function alternate($text)
{
$this->alternate = $text;
return $this;
}
/**
* Sets the prefix text.
*
* @param string $text
* @return Feather\Components\Support\Date
*/
public function prefix($text)
{
$this->prefix = $text;
return $this;
}
/**
* Sets the suffix text.
*
* @param string $text
* @return Feather\Components\Support\Date
*/
public function suffix($text)
{
$this->suffix = $text;
return $this;
}
/**
* Returns the formatted date.
*
* @param string $format
* @return string
*/
public function show($format)
{
$formats = array(
'long' => $this->feather['config']->get('feather: db.datetime.long_date'),
'short' => $this->feather['config']->get('feather: db.datetime.short_date'),
'time' => $this->feather['config']->get('feather: db.datetime.time_only')
);
if(isset($formats[$format]))
{
$format = $formats[$format];
}
$errors = $this->date->getLastErrors();
if($this->date->getTimestamp() == 0 or $errors['warning_count'] > 0 or $errors['error_count'] > 0)
{
return $this->alternate ?: 'Invalid date supplied.';
}
return $this->prefix . $this->date->format($format) . $this->suffix;
}
/**
* Returns a fuzzy formated date.
*
* @return string
*/
public function fuzzy($stop = 'week', $format = 'long')
{
$difference = time() - $this->date->getTimestamp();
$offset = array_search($stop, $this->fuzzy['periods']) + 1;
$periods = array_slice($this->fuzzy['periods'], 0, $offset);
$lengths = array_slice($this->fuzzy['lengths'], 0, $offset);
for($i = 0; $difference >= $lengths[$i] and $i < count($lengths) - 1; $i++)
{
$difference = $difference / $lengths[$i];
}
$difference = round($difference);
if($difference != 1)
{
$periods[$i] .= 's';
}
if($difference > $lengths[$i])
{
return $this->show($format);
}
return $this->prefix . number_format($difference) . ' ' . $periods[$i] . ' ago' . $this->suffix;
}
/**
* Returns a relative formated date.
*
* @return string
*/
public function relative($format = 'long')
{
$days = intval((time() - $this->date->getTimestamp()) / 86400);
if($days == 0)
{
return __('feather core::common.today')->get() . ', ' . $this->show('time');
}
elseif($days == 1)
{
return __('feather core::common.yesterday')->get() . ', ' . $this->show('time');
}
return $this->show($format);
}
}