/
TimeHelper.php
357 lines (329 loc) · 13.6 KB
/
TimeHelper.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 0.10.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\View\Helper;
use Cake\Utility\Time;
use Cake\View\Helper;
use Cake\View\Helper\StringTemplateTrait;
/**
* Time Helper class for easy use of time data.
*
* Manipulation of time data.
*
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html
* @see \Cake\Utility\Time
*/
class TimeHelper extends Helper {
use StringTemplateTrait;
/**
* Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
*
* @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @return Cake\Utility\Time
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function fromString($dateString, $timezone = null) {
return (new Time($dateString))->timezone($timezone);
}
/**
* Returns a nicely formatted date string for given Datetime string.
*
* @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @param string $format The format to use. If null, `CakeTime::$niceFormat` is used
* @return string Formatted date string
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function nice($dateString = null, $timezone = null, $locale = null) {
return (new Time($dateString))->nice($timezone, $locale);
}
/**
* Returns true if given datetime string is today.
*
* @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @return bool True if datetime string is today
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public function isToday($dateString, $timezone = null) {
return (new Time($dateString, $timezone))->isToday();
}
/**
* Returns true if given datetime string is in the future.
*
* @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @return bool True if datetime string is today
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public function isFuture($dateString, $timezone = null) {
return (new Time($dateString, $timezone))->isFuture();
}
/**
* Returns true if given datetime string is in the past.
*
* @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @return bool True if datetime string is today
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public function isPast($dateString, $timezone = null) {
return (new Time($dateString, $timezone))->isPast();
}
/**
* Returns true if given datetime string is within this week.
*
* @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @return bool True if datetime string is within current week
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public function isThisWeek($dateString, $timezone = null) {
return (new Time($dateString, $timezone))->isThisWeek();
}
/**
* Returns true if given datetime string is within this month
*
* @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @return bool True if datetime string is within current month
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public function isThisMonth($dateString, $timezone = null) {
return (new Time($dateString, $timezone))->isThisMonth();
}
/**
* Returns true if given datetime string is within current year.
*
* @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @return bool True if datetime string is within current year
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public function isThisYear($dateString, $timezone = null) {
return (new Time($dateString, $timezone))->isThisYear();
}
/**
* Returns true if given datetime string was yesterday.
*
* @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @return bool True if datetime string was yesterday
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*
*/
public function wasYesterday($dateString, $timezone = null) {
return (new Time($dateString, $timezone))->isYesterday();
}
/**
* Returns true if given datetime string is tomorrow.
*
* @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @return bool True if datetime string was yesterday
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public function isTomorrow($dateString, $timezone = null) {
return (new Time($dateString, $timezone))->isTomorrow();
}
/**
* Returns the quarter
*
* @see \Cake\Utility\Time::toQuarter()
*
* @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param bool $range if true returns a range in Y-m-d format
* @return mixed 1, 2, 3, or 4 quarter of year or array if $range true
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function toQuarter($dateString, $range = false) {
return (new Time($dateString))->toQuarter($range);
}
/**
* Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime().
*
* @see \Cake\Utility\Time::toUnix()
*
* @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @return int Unix timestamp
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function toUnix($dateString, $timezone = null) {
return (new Time($dateString, $timezone))->toUnixString();
}
/**
* Returns a date formatted for Atom RSS feeds.
*
* @see \Cake\Utility\Time::toAtom()
*
* @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @return string Formatted date string
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function toAtom($dateString, $timezone = null) {
$timezone = $timezone ?: date_default_timezone_get();
return (new Time($dateString))->timezone($timezone)->toATOMString();
}
/**
* Formats date for RSS feeds
*
* @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @return string Formatted date string
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function toRSS($dateString, $timezone = null) {
$timezone = $timezone ?: date_default_timezone_get();
return (new Time($dateString))->timezone($timezone)->toRSSString();
}
/**
* Formats date for RSS feeds
*
* @see \Cake\Utility\Time::timeAgoInWords()
*
* ## Additional options
*
* - `element` - The element to wrap the formatted time in.
* Has a few additional options:
* - `tag` - The tag to use, defaults to 'span'.
* - `class` - The class name to use, defaults to `time-ago-in-words`.
* - `title` - Defaults to the $dateTime input.
*
* @param int|string|\DateTime $dateTime UNIX timestamp, strtotime() valid string or DateTime object
* @param array $options Default format if timestamp is used in $dateString
* @return string Relative time string.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function timeAgoInWords($dateTime, array $options = array()) {
$element = null;
if (!empty($options['element'])) {
$element = array(
'tag' => 'span',
'class' => 'time-ago-in-words',
'title' => $dateTime
);
if (is_array($options['element'])) {
$element = $options['element'] + $element;
} else {
$element['tag'] = $options['element'];
}
unset($options['element']);
}
$relativeDate = (new Time($dateTime))->timeAgoInWords($options);
if ($element) {
$relativeDate = sprintf(
'<%s%s>%s</%s>',
$element['tag'],
$this->templater()->formatAttributes($element, array('tag')),
$relativeDate,
$element['tag']
);
}
return $relativeDate;
}
/**
* Returns true if specified datetime was within the interval specified, else false.
* @see \Cake\Utility\Time::wasWithinLast()
*
* @param string|int $timeInterval the numeric value with space then time type.
* Example of valid types: 6 hours, 2 days, 1 minute.
* @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @return bool
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public function wasWithinLast($timeInterval, $dateString, $timezone = null) {
return (new Time($dateString, $timezone))->wasWithinLast($timeInterval);
}
/**
* Returns true if specified datetime is within the interval specified, else false.
*
* @see \Cake\Utility\Time::isWithinLast()
*
* @param string|int $timeInterval the numeric value with space then time type.
* Example of valid types: 6 hours, 2 days, 1 minute.
* @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @return bool
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public function isWithinNext($timeInterval, $dateString, $timezone = null) {
return (new Time($dateString, $timezone))->isWithinNext($timeInterval);
}
/**
* Returns gmt as a UNIX timestamp.
*
* @see \Cake\Utility\Time::gmt()
*
* @param int|string|\DateTime $string UNIX timestamp, strtotime() valid string or DateTime object
* @return int UNIX timestamp
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function gmt($string = null) {
return (new Time($string))->toUnixString();
}
/**
* Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string.
* This function also accepts a time string and a format string as first and second parameters.
* In that case this function behaves as a wrapper for Time::i18nFormat()
*
* @see \Cake\Utility\Time::i18nFormat()
*
* @param int|string|\DateTime $date UNIX timestamp, strtotime() valid string or DateTime object (or a date format string)
* @param int|string $format date format string (or a UNIX timestamp, strtotime() valid string or DateTime object)
* @param bool|string $invalid Default value to display on invalid dates
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @return string Formatted and translated date string
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/
public function format($date, $format = null, $invalid = false, $timezone = null) {
return $this->i18nFormat($date, $format, $invalid, $timezone);
}
/**
* Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string.
* It takes into account the default date format for the current language if a LC_TIME file is used.
*
* @see \Cake\Utility\Time::i18nFormat()
*
* @param int|string|\DateTime $date UNIX timestamp, strtotime() valid string or DateTime object
* @param string $format strftime format string.
* @param bool|string $invalid Default value to display on invalid dates
* @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
* @return string Formatted and translated date string
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
* @throws \InvalidArgumentException When the date cannot be parsed
*/
public function i18nFormat($date, $format = null, $invalid = false, $timezone = null) {
try {
$time = new Time($date, $timezone);
return $time->i18nFormat($format, $timezone);
} catch (\Exception $e) {
if ($invalid === false) {
throw $e;
}
return $invalid;
}
}
/**
* Event listeners.
*
* @return array
*/
public function implementedEvents() {
return [];
}
}