1 | <?php |
---|
2 | /** |
---|
3 | * Time Helper class file. |
---|
4 | * |
---|
5 | * PHP versions 4 and 5 |
---|
6 | * |
---|
7 | * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
---|
8 | * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) |
---|
9 | * |
---|
10 | * Licensed under The MIT License |
---|
11 | * Redistributions of files must retain the above copyright notice. |
---|
12 | * |
---|
13 | * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) |
---|
14 | * @link http://cakephp.org CakePHP(tm) Project |
---|
15 | * @package cake |
---|
16 | * @subpackage cake.cake.libs.view.helpers |
---|
17 | * @since CakePHP(tm) v 0.10.0.1076 |
---|
18 | * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
---|
19 | */ |
---|
20 | |
---|
21 | /** |
---|
22 | * Time Helper class for easy use of time data. |
---|
23 | * |
---|
24 | * Manipulation of time data. |
---|
25 | * |
---|
26 | * @package cake |
---|
27 | * @subpackage cake.cake.libs.view.helpers |
---|
28 | * @link http://book.cakephp.org/view/1470/Time |
---|
29 | */ |
---|
30 | class TimeHelper extends AppHelper { |
---|
31 | |
---|
32 | /** |
---|
33 | * Converts a string representing the format for the function strftime and returns a |
---|
34 | * windows safe and i18n aware format. |
---|
35 | * |
---|
36 | * @param string $format Format with specifiers for strftime function. |
---|
37 | * Accepts the special specifier %S which mimics th modifier S for date() |
---|
38 | * @param string UNIX timestamp |
---|
39 | * @return string windows safe and date() function compatible format for strftime |
---|
40 | * @access public |
---|
41 | */ |
---|
42 | function convertSpecifiers($format, $time = null) { |
---|
43 | if (!$time) { |
---|
44 | $time = time(); |
---|
45 | } |
---|
46 | $this->__time = $time; |
---|
47 | return preg_replace_callback('/\%(\w+)/', array($this, '__translateSpecifier'), $format); |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * Auxiliary function to translate a matched specifier element from a regular expresion into |
---|
52 | * a windows safe and i18n aware specifier |
---|
53 | * |
---|
54 | * @param array $specifier match from regular expression |
---|
55 | * @return string converted element |
---|
56 | * @access private |
---|
57 | */ |
---|
58 | function __translateSpecifier($specifier) { |
---|
59 | switch ($specifier[1]) { |
---|
60 | case 'a': |
---|
61 | $abday = __c('abday', 5, true); |
---|
62 | if (is_array($abday)) { |
---|
63 | return $abday[date('w', $this->__time)]; |
---|
64 | } |
---|
65 | break; |
---|
66 | case 'A': |
---|
67 | $day = __c('day',5,true); |
---|
68 | if (is_array($day)) { |
---|
69 | return $day[date('w', $this->__time)]; |
---|
70 | } |
---|
71 | break; |
---|
72 | case 'c': |
---|
73 | $format = __c('d_t_fmt',5,true); |
---|
74 | if ($format != 'd_t_fmt') { |
---|
75 | return $this->convertSpecifiers($format, $this->__time); |
---|
76 | } |
---|
77 | break; |
---|
78 | case 'C': |
---|
79 | return sprintf("%02d", date('Y', $this->__time) / 100); |
---|
80 | case 'D': |
---|
81 | return '%m/%d/%y'; |
---|
82 | case 'e': |
---|
83 | if (DS === '/') { |
---|
84 | return '%e'; |
---|
85 | } |
---|
86 | $day = date('j', $this->__time); |
---|
87 | if ($day < 10) { |
---|
88 | $day = ' ' . $day; |
---|
89 | } |
---|
90 | return $day; |
---|
91 | case 'eS' : |
---|
92 | return date('jS', $this->__time); |
---|
93 | case 'b': |
---|
94 | case 'h': |
---|
95 | $months = __c('abmon', 5, true); |
---|
96 | if (is_array($months)) { |
---|
97 | return $months[date('n', $this->__time) -1]; |
---|
98 | } |
---|
99 | return '%b'; |
---|
100 | case 'B': |
---|
101 | $months = __c('mon',5,true); |
---|
102 | if (is_array($months)) { |
---|
103 | return $months[date('n', $this->__time) -1]; |
---|
104 | } |
---|
105 | break; |
---|
106 | case 'n': |
---|
107 | return "\n"; |
---|
108 | case 'p': |
---|
109 | case 'P': |
---|
110 | $default = array('am' => 0, 'pm' => 1); |
---|
111 | $meridiem = $default[date('a',$this->__time)]; |
---|
112 | $format = __c('am_pm', 5, true); |
---|
113 | if (is_array($format)) { |
---|
114 | $meridiem = $format[$meridiem]; |
---|
115 | return ($specifier[1] == 'P') ? strtolower($meridiem) : strtoupper($meridiem); |
---|
116 | } |
---|
117 | break; |
---|
118 | case 'r': |
---|
119 | $complete = __c('t_fmt_ampm', 5, true); |
---|
120 | if ($complete != 't_fmt_ampm') { |
---|
121 | return str_replace('%p',$this->__translateSpecifier(array('%p', 'p')),$complete); |
---|
122 | } |
---|
123 | break; |
---|
124 | case 'R': |
---|
125 | return date('H:i', $this->__time); |
---|
126 | case 't': |
---|
127 | return "\t"; |
---|
128 | case 'T': |
---|
129 | return '%H:%M:%S'; |
---|
130 | case 'u': |
---|
131 | return ($weekDay = date('w', $this->__time)) ? $weekDay : 7; |
---|
132 | case 'x': |
---|
133 | $format = __c('d_fmt', 5, true); |
---|
134 | if ($format != 'd_fmt') { |
---|
135 | return $this->convertSpecifiers($format, $this->__time); |
---|
136 | } |
---|
137 | break; |
---|
138 | case 'X': |
---|
139 | $format = __c('t_fmt',5,true); |
---|
140 | if ($format != 't_fmt') { |
---|
141 | return $this->convertSpecifiers($format, $this->__time); |
---|
142 | } |
---|
143 | break; |
---|
144 | } |
---|
145 | return $specifier[0]; |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * Converts given time (in server's time zone) to user's local time, given his/her offset from GMT. |
---|
150 | * |
---|
151 | * @param string $serverTime UNIX timestamp |
---|
152 | * @param int $userOffset User's offset from GMT (in hours) |
---|
153 | * @return string UNIX timestamp |
---|
154 | * @access public |
---|
155 | */ |
---|
156 | function convert($serverTime, $userOffset) { |
---|
157 | $serverOffset = $this->serverOffset(); |
---|
158 | $gmtTime = $serverTime - $serverOffset; |
---|
159 | $userTime = $gmtTime + $userOffset * (60*60); |
---|
160 | return $userTime; |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * Returns server's offset from GMT in seconds. |
---|
165 | * |
---|
166 | * @return int Offset |
---|
167 | * @access public |
---|
168 | */ |
---|
169 | function serverOffset() { |
---|
170 | return date('Z', time()); |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string. |
---|
175 | * |
---|
176 | * @param string $dateString Datetime string |
---|
177 | * @param int $userOffset User's offset from GMT (in hours) |
---|
178 | * @return string Parsed timestamp |
---|
179 | * @access public |
---|
180 | * @link http://book.cakephp.org/view/1471/Formatting |
---|
181 | */ |
---|
182 | function fromString($dateString, $userOffset = null) { |
---|
183 | if (empty($dateString)) { |
---|
184 | return false; |
---|
185 | } |
---|
186 | if (is_integer($dateString) || is_numeric($dateString)) { |
---|
187 | $date = intval($dateString); |
---|
188 | } else { |
---|
189 | $date = strtotime($dateString); |
---|
190 | } |
---|
191 | if ($userOffset !== null) { |
---|
192 | return $this->convert($date, $userOffset); |
---|
193 | } |
---|
194 | if ($date === -1) { |
---|
195 | return false; |
---|
196 | } |
---|
197 | return $date; |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * Returns a nicely formatted date string for given Datetime string. |
---|
202 | * |
---|
203 | * @param string $dateString Datetime string or Unix timestamp |
---|
204 | * @param int $userOffset User's offset from GMT (in hours) |
---|
205 | * @return string Formatted date string |
---|
206 | * @access public |
---|
207 | * @link http://book.cakephp.org/view/1471/Formatting |
---|
208 | */ |
---|
209 | function nice($dateString = null, $userOffset = null) { |
---|
210 | if ($dateString != null) { |
---|
211 | $date = $this->fromString($dateString, $userOffset); |
---|
212 | } else { |
---|
213 | $date = time(); |
---|
214 | } |
---|
215 | $format = $this->convertSpecifiers('%a, %b %eS %Y, %H:%M', $date); |
---|
216 | return strftime($format, $date); |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * Returns a formatted descriptive date string for given datetime string. |
---|
221 | * |
---|
222 | * If the given date is today, the returned string could be "Today, 16:54". |
---|
223 | * If the given date was yesterday, the returned string could be "Yesterday, 16:54". |
---|
224 | * If $dateString's year is the current year, the returned string does not |
---|
225 | * include mention of the year. |
---|
226 | * |
---|
227 | * @param string $dateString Datetime string or Unix timestamp |
---|
228 | * @param int $userOffset User's offset from GMT (in hours) |
---|
229 | * @return string Described, relative date string |
---|
230 | * @access public |
---|
231 | * @link http://book.cakephp.org/view/1471/Formatting |
---|
232 | */ |
---|
233 | function niceShort($dateString = null, $userOffset = null) { |
---|
234 | $date = $dateString ? $this->fromString($dateString, $userOffset) : time(); |
---|
235 | |
---|
236 | $y = $this->isThisYear($date) ? '' : ' %Y'; |
---|
237 | |
---|
238 | if ($this->isToday($dateString, $userOffset)) { |
---|
239 | $ret = sprintf(__('Today, %s',true), strftime("%H:%M", $date)); |
---|
240 | } elseif ($this->wasYesterday($dateString, $userOffset)) { |
---|
241 | $ret = sprintf(__('Yesterday, %s',true), strftime("%H:%M", $date)); |
---|
242 | } else { |
---|
243 | $format = $this->convertSpecifiers("%b %eS{$y}, %H:%M", $date); |
---|
244 | $ret = strftime($format, $date); |
---|
245 | } |
---|
246 | |
---|
247 | return $ret; |
---|
248 | } |
---|
249 | |
---|
250 | /** |
---|
251 | * Returns a partial SQL string to search for all records between two dates. |
---|
252 | * |
---|
253 | * @param string $dateString Datetime string or Unix timestamp |
---|
254 | * @param string $end Datetime string or Unix timestamp |
---|
255 | * @param string $fieldName Name of database field to compare with |
---|
256 | * @param int $userOffset User's offset from GMT (in hours) |
---|
257 | * @return string Partial SQL string. |
---|
258 | * @access public |
---|
259 | * @link http://book.cakephp.org/view/1471/Formatting |
---|
260 | */ |
---|
261 | function daysAsSql($begin, $end, $fieldName, $userOffset = null) { |
---|
262 | $begin = $this->fromString($begin, $userOffset); |
---|
263 | $end = $this->fromString($end, $userOffset); |
---|
264 | $begin = date('Y-m-d', $begin) . ' 00:00:00'; |
---|
265 | $end = date('Y-m-d', $end) . ' 23:59:59'; |
---|
266 | |
---|
267 | return "($fieldName >= '$begin') AND ($fieldName <= '$end')"; |
---|
268 | } |
---|
269 | |
---|
270 | /** |
---|
271 | * Returns a partial SQL string to search for all records between two times |
---|
272 | * occurring on the same day. |
---|
273 | * |
---|
274 | * @param string $dateString Datetime string or Unix timestamp |
---|
275 | * @param string $fieldName Name of database field to compare with |
---|
276 | * @param int $userOffset User's offset from GMT (in hours) |
---|
277 | * @return string Partial SQL string. |
---|
278 | * @access public |
---|
279 | * @link http://book.cakephp.org/view/1471/Formatting |
---|
280 | */ |
---|
281 | function dayAsSql($dateString, $fieldName, $userOffset = null) { |
---|
282 | $date = $this->fromString($dateString, $userOffset); |
---|
283 | return $this->daysAsSql($dateString, $dateString, $fieldName); |
---|
284 | } |
---|
285 | |
---|
286 | /** |
---|
287 | * Returns true if given datetime string is today. |
---|
288 | * |
---|
289 | * @param string $dateString Datetime string or Unix timestamp |
---|
290 | * @param int $userOffset User's offset from GMT (in hours) |
---|
291 | * @return boolean True if datetime string is today |
---|
292 | * @access public |
---|
293 | */ |
---|
294 | function isToday($dateString, $userOffset = null) { |
---|
295 | $date = $this->fromString($dateString, $userOffset); |
---|
296 | return date('Y-m-d', $date) == date('Y-m-d', time()); |
---|
297 | } |
---|
298 | |
---|
299 | /** |
---|
300 | * Returns true if given datetime string is within this week |
---|
301 | * @param string $dateString |
---|
302 | * @param int $userOffset User's offset from GMT (in hours) |
---|
303 | * @return boolean True if datetime string is within current week |
---|
304 | * @access public |
---|
305 | * @link http://book.cakephp.org/view/1472/Testing-Time |
---|
306 | */ |
---|
307 | function isThisWeek($dateString, $userOffset = null) { |
---|
308 | $date = $this->fromString($dateString, $userOffset); |
---|
309 | return date('W Y', $date) == date('W Y', time()); |
---|
310 | } |
---|
311 | |
---|
312 | /** |
---|
313 | * Returns true if given datetime string is within this month |
---|
314 | * @param string $dateString |
---|
315 | * @param int $userOffset User's offset from GMT (in hours) |
---|
316 | * @return boolean True if datetime string is within current month |
---|
317 | * @access public |
---|
318 | * @link http://book.cakephp.org/view/1472/Testing-Time |
---|
319 | */ |
---|
320 | function isThisMonth($dateString, $userOffset = null) { |
---|
321 | $date = $this->fromString($dateString); |
---|
322 | return date('m Y',$date) == date('m Y', time()); |
---|
323 | } |
---|
324 | |
---|
325 | /** |
---|
326 | * Returns true if given datetime string is within current year. |
---|
327 | * |
---|
328 | * @param string $dateString Datetime string or Unix timestamp |
---|
329 | * @return boolean True if datetime string is within current year |
---|
330 | * @access public |
---|
331 | * @link http://book.cakephp.org/view/1472/Testing-Time |
---|
332 | */ |
---|
333 | function isThisYear($dateString, $userOffset = null) { |
---|
334 | $date = $this->fromString($dateString, $userOffset); |
---|
335 | return date('Y', $date) == date('Y', time()); |
---|
336 | } |
---|
337 | |
---|
338 | /** |
---|
339 | * Returns true if given datetime string was yesterday. |
---|
340 | * |
---|
341 | * @param string $dateString Datetime string or Unix timestamp |
---|
342 | * @param int $userOffset User's offset from GMT (in hours) |
---|
343 | * @return boolean True if datetime string was yesterday |
---|
344 | * @access public |
---|
345 | * @link http://book.cakephp.org/view/1472/Testing-Time |
---|
346 | * |
---|
347 | */ |
---|
348 | function wasYesterday($dateString, $userOffset = null) { |
---|
349 | $date = $this->fromString($dateString, $userOffset); |
---|
350 | return date('Y-m-d', $date) == date('Y-m-d', strtotime('yesterday')); |
---|
351 | } |
---|
352 | |
---|
353 | /** |
---|
354 | * Returns true if given datetime string is tomorrow. |
---|
355 | * |
---|
356 | * @param string $dateString Datetime string or Unix timestamp |
---|
357 | * @param int $userOffset User's offset from GMT (in hours) |
---|
358 | * @return boolean True if datetime string was yesterday |
---|
359 | * @access public |
---|
360 | * @link http://book.cakephp.org/view/1472/Testing-Time |
---|
361 | */ |
---|
362 | function isTomorrow($dateString, $userOffset = null) { |
---|
363 | $date = $this->fromString($dateString, $userOffset); |
---|
364 | return date('Y-m-d', $date) == date('Y-m-d', strtotime('tomorrow')); |
---|
365 | } |
---|
366 | |
---|
367 | /** |
---|
368 | * Returns the quarter |
---|
369 | * |
---|
370 | * @param string $dateString |
---|
371 | * @param boolean $range if true returns a range in Y-m-d format |
---|
372 | * @return boolean True if datetime string is within current week |
---|
373 | * @access public |
---|
374 | * @link http://book.cakephp.org/view/1471/Formatting |
---|
375 | */ |
---|
376 | function toQuarter($dateString, $range = false) { |
---|
377 | $time = $this->fromString($dateString); |
---|
378 | $date = ceil(date('m', $time) / 3); |
---|
379 | |
---|
380 | if ($range === true) { |
---|
381 | $range = 'Y-m-d'; |
---|
382 | } |
---|
383 | |
---|
384 | if ($range !== false) { |
---|
385 | $year = date('Y', $time); |
---|
386 | |
---|
387 | switch ($date) { |
---|
388 | case 1: |
---|
389 | $date = array($year.'-01-01', $year.'-03-31'); |
---|
390 | break; |
---|
391 | case 2: |
---|
392 | $date = array($year.'-04-01', $year.'-06-30'); |
---|
393 | break; |
---|
394 | case 3: |
---|
395 | $date = array($year.'-07-01', $year.'-09-30'); |
---|
396 | break; |
---|
397 | case 4: |
---|
398 | $date = array($year.'-10-01', $year.'-12-31'); |
---|
399 | break; |
---|
400 | } |
---|
401 | } |
---|
402 | return $date; |
---|
403 | } |
---|
404 | |
---|
405 | /** |
---|
406 | * Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime(). |
---|
407 | * |
---|
408 | * @param string $dateString Datetime string to be represented as a Unix timestamp |
---|
409 | * @param int $userOffset User's offset from GMT (in hours) |
---|
410 | * @return integer Unix timestamp |
---|
411 | * @access public |
---|
412 | * @link http://book.cakephp.org/view/1471/Formatting |
---|
413 | */ |
---|
414 | function toUnix($dateString, $userOffset = null) { |
---|
415 | return $this->fromString($dateString, $userOffset); |
---|
416 | } |
---|
417 | |
---|
418 | /** |
---|
419 | * Returns a date formatted for Atom RSS feeds. |
---|
420 | * |
---|
421 | * @param string $dateString Datetime string or Unix timestamp |
---|
422 | * @param int $userOffset User's offset from GMT (in hours) |
---|
423 | * @return string Formatted date string |
---|
424 | * @access public |
---|
425 | * @link http://book.cakephp.org/view/1471/Formatting |
---|
426 | */ |
---|
427 | function toAtom($dateString, $userOffset = null) { |
---|
428 | $date = $this->fromString($dateString, $userOffset); |
---|
429 | return date('Y-m-d\TH:i:s\Z', $date); |
---|
430 | } |
---|
431 | |
---|
432 | /** |
---|
433 | * Formats date for RSS feeds |
---|
434 | * |
---|
435 | * @param string $dateString Datetime string or Unix timestamp |
---|
436 | * @param int $userOffset User's offset from GMT (in hours) |
---|
437 | * @return string Formatted date string |
---|
438 | * @access public |
---|
439 | * @link http://book.cakephp.org/view/1471/Formatting |
---|
440 | */ |
---|
441 | function toRSS($dateString, $userOffset = null) { |
---|
442 | $date = $this->fromString($dateString, $userOffset); |
---|
443 | |
---|
444 | if(!is_null($userOffset)) { |
---|
445 | if($userOffset == 0) { |
---|
446 | $timezone = '+0000'; |
---|
447 | } else { |
---|
448 | $hours = (int) floor(abs($userOffset)); |
---|
449 | $minutes = (int) (fmod(abs($userOffset), $hours) * 60); |
---|
450 | $timezone = ($userOffset < 0 ? '-' : '+') . str_pad($hours, 2, '0', STR_PAD_LEFT) . str_pad($minutes, 2, '0', STR_PAD_LEFT); |
---|
451 | } |
---|
452 | return date('D, d M Y H:i:s', $date) . ' ' . $timezone; |
---|
453 | } |
---|
454 | return date("r", $date); |
---|
455 | } |
---|
456 | |
---|
457 | /** |
---|
458 | * Returns either a relative date or a formatted date depending |
---|
459 | * on the difference between the current time and given datetime. |
---|
460 | * $datetime should be in a <i>strtotime</i> - parsable format, like MySQL's datetime datatype. |
---|
461 | * |
---|
462 | * ### Options: |
---|
463 | * |
---|
464 | * - `format` => a fall back format if the relative time is longer than the duration specified by end |
---|
465 | * - `end` => The end of relative time telling |
---|
466 | * - `userOffset` => Users offset from GMT (in hours) |
---|
467 | * |
---|
468 | * Relative dates look something like this: |
---|
469 | * 3 weeks, 4 days ago |
---|
470 | * 15 seconds ago |
---|
471 | * |
---|
472 | * Default date formatting is d/m/yy e.g: on 18/2/09 |
---|
473 | * |
---|
474 | * The returned string includes 'ago' or 'on' and assumes you'll properly add a word |
---|
475 | * like 'Posted ' before the function output. |
---|
476 | * |
---|
477 | * @param string $dateString Datetime string or Unix timestamp |
---|
478 | * @param array $options Default format if timestamp is used in $dateString |
---|
479 | * @return string Relative time string. |
---|
480 | * @access public |
---|
481 | * @link http://book.cakephp.org/view/1471/Formatting |
---|
482 | */ |
---|
483 | function timeAgoInWords($dateTime, $options = array()) { |
---|
484 | $userOffset = null; |
---|
485 | if (is_array($options) && isset($options['userOffset'])) { |
---|
486 | $userOffset = $options['userOffset']; |
---|
487 | } |
---|
488 | $now = time(); |
---|
489 | if (!is_null($userOffset)) { |
---|
490 | $now = $this->convert(time(), $userOffset); |
---|
491 | } |
---|
492 | $inSeconds = $this->fromString($dateTime, $userOffset); |
---|
493 | $backwards = ($inSeconds > $now); |
---|
494 | |
---|
495 | $format = 'j/n/y'; |
---|
496 | $end = '+1 month'; |
---|
497 | |
---|
498 | if (is_array($options)) { |
---|
499 | if (isset($options['format'])) { |
---|
500 | $format = $options['format']; |
---|
501 | unset($options['format']); |
---|
502 | } |
---|
503 | if (isset($options['end'])) { |
---|
504 | $end = $options['end']; |
---|
505 | unset($options['end']); |
---|
506 | } |
---|
507 | } else { |
---|
508 | $format = $options; |
---|
509 | } |
---|
510 | |
---|
511 | if ($backwards) { |
---|
512 | $futureTime = $inSeconds; |
---|
513 | $pastTime = $now; |
---|
514 | } else { |
---|
515 | $futureTime = $now; |
---|
516 | $pastTime = $inSeconds; |
---|
517 | } |
---|
518 | $diff = $futureTime - $pastTime; |
---|
519 | |
---|
520 | // If more than a week, then take into account the length of months |
---|
521 | if ($diff >= 604800) { |
---|
522 | $current = array(); |
---|
523 | $date = array(); |
---|
524 | |
---|
525 | list($future['H'], $future['i'], $future['s'], $future['d'], $future['m'], $future['Y']) = explode('/', date('H/i/s/d/m/Y', $futureTime)); |
---|
526 | |
---|
527 | list($past['H'], $past['i'], $past['s'], $past['d'], $past['m'], $past['Y']) = explode('/', date('H/i/s/d/m/Y', $pastTime)); |
---|
528 | $years = $months = $weeks = $days = $hours = $minutes = $seconds = 0; |
---|
529 | |
---|
530 | if ($future['Y'] == $past['Y'] && $future['m'] == $past['m']) { |
---|
531 | $months = 0; |
---|
532 | $years = 0; |
---|
533 | } else { |
---|
534 | if ($future['Y'] == $past['Y']) { |
---|
535 | $months = $future['m'] - $past['m']; |
---|
536 | } else { |
---|
537 | $years = $future['Y'] - $past['Y']; |
---|
538 | $months = $future['m'] + ((12 * $years) - $past['m']); |
---|
539 | |
---|
540 | if ($months >= 12) { |
---|
541 | $years = floor($months / 12); |
---|
542 | $months = $months - ($years * 12); |
---|
543 | } |
---|
544 | |
---|
545 | if ($future['m'] < $past['m'] && $future['Y'] - $past['Y'] == 1) { |
---|
546 | $years --; |
---|
547 | } |
---|
548 | } |
---|
549 | } |
---|
550 | |
---|
551 | if ($future['d'] >= $past['d']) { |
---|
552 | $days = $future['d'] - $past['d']; |
---|
553 | } else { |
---|
554 | $daysInPastMonth = date('t', $pastTime); |
---|
555 | $daysInFutureMonth = date('t', mktime(0, 0, 0, $future['m'] - 1, 1, $future['Y'])); |
---|
556 | |
---|
557 | if (!$backwards) { |
---|
558 | $days = ($daysInPastMonth - $past['d']) + $future['d']; |
---|
559 | } else { |
---|
560 | $days = ($daysInFutureMonth - $past['d']) + $future['d']; |
---|
561 | } |
---|
562 | |
---|
563 | if ($future['m'] != $past['m']) { |
---|
564 | $months --; |
---|
565 | } |
---|
566 | } |
---|
567 | |
---|
568 | if ($months == 0 && $years >= 1 && $diff < ($years * 31536000)) { |
---|
569 | $months = 11; |
---|
570 | $years --; |
---|
571 | } |
---|
572 | |
---|
573 | if ($months >= 12) { |
---|
574 | $years = $years + 1; |
---|
575 | $months = $months - 12; |
---|
576 | } |
---|
577 | |
---|
578 | if ($days >= 7) { |
---|
579 | $weeks = floor($days / 7); |
---|
580 | $days = $days - ($weeks * 7); |
---|
581 | } |
---|
582 | } else { |
---|
583 | $years = $months = $weeks = 0; |
---|
584 | $days = floor($diff / 86400); |
---|
585 | |
---|
586 | $diff = $diff - ($days * 86400); |
---|
587 | |
---|
588 | $hours = floor($diff / 3600); |
---|
589 | $diff = $diff - ($hours * 3600); |
---|
590 | |
---|
591 | $minutes = floor($diff / 60); |
---|
592 | $diff = $diff - ($minutes * 60); |
---|
593 | $seconds = $diff; |
---|
594 | } |
---|
595 | $relativeDate = ''; |
---|
596 | $diff = $futureTime - $pastTime; |
---|
597 | |
---|
598 | if ($diff > abs($now - $this->fromString($end))) { |
---|
599 | $relativeDate = sprintf(__('on %s',true), date($format, $inSeconds)); |
---|
600 | } else { |
---|
601 | if ($years > 0) { |
---|
602 | // years and months and days |
---|
603 | $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d year', '%d years', $years, true), $years); |
---|
604 | $relativeDate .= $months > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d month', '%d months', $months, true), $months) : ''; |
---|
605 | $relativeDate .= $weeks > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d week', '%d weeks', $weeks, true), $weeks) : ''; |
---|
606 | $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d day', '%d days', $days, true), $days) : ''; |
---|
607 | } elseif (abs($months) > 0) { |
---|
608 | // months, weeks and days |
---|
609 | $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d month', '%d months', $months, true), $months); |
---|
610 | $relativeDate .= $weeks > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d week', '%d weeks', $weeks, true), $weeks) : ''; |
---|
611 | $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d day', '%d days', $days, true), $days) : ''; |
---|
612 | } elseif (abs($weeks) > 0) { |
---|
613 | // weeks and days |
---|
614 | $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d week', '%d weeks', $weeks, true), $weeks); |
---|
615 | $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d day', '%d days', $days, true), $days) : ''; |
---|
616 | } elseif (abs($days) > 0) { |
---|
617 | // days and hours |
---|
618 | $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d day', '%d days', $days, true), $days); |
---|
619 | $relativeDate .= $hours > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d hour', '%d hours', $hours, true), $hours) : ''; |
---|
620 | } elseif (abs($hours) > 0) { |
---|
621 | // hours and minutes |
---|
622 | $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d hour', '%d hours', $hours, true), $hours); |
---|
623 | $relativeDate .= $minutes > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d minute', '%d minutes', $minutes, true), $minutes) : ''; |
---|
624 | } elseif (abs($minutes) > 0) { |
---|
625 | // minutes only |
---|
626 | $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d minute', '%d minutes', $minutes, true), $minutes); |
---|
627 | } else { |
---|
628 | // seconds only |
---|
629 | $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d second', '%d seconds', $seconds, true), $seconds); |
---|
630 | } |
---|
631 | |
---|
632 | if (!$backwards) { |
---|
633 | $relativeDate = sprintf(__('%s ago', true), $relativeDate); |
---|
634 | } |
---|
635 | } |
---|
636 | return $relativeDate; |
---|
637 | } |
---|
638 | |
---|
639 | /** |
---|
640 | * Alias for timeAgoInWords |
---|
641 | * |
---|
642 | * @param mixed $dateTime Datetime string (strtotime-compatible) or Unix timestamp |
---|
643 | * @param mixed $options Default format string, if timestamp is used in $dateTime, or an array of options to be passed |
---|
644 | * on to timeAgoInWords(). |
---|
645 | * @return string Relative time string. |
---|
646 | * @see TimeHelper::timeAgoInWords |
---|
647 | * @access public |
---|
648 | * @deprecated This method alias will be removed in future versions. |
---|
649 | * @link http://book.cakephp.org/view/1471/Formatting |
---|
650 | */ |
---|
651 | function relativeTime($dateTime, $options = array()) { |
---|
652 | return $this->timeAgoInWords($dateTime, $options); |
---|
653 | } |
---|
654 | |
---|
655 | /** |
---|
656 | * Returns true if specified datetime was within the interval specified, else false. |
---|
657 | * |
---|
658 | * @param mixed $timeInterval the numeric value with space then time type. |
---|
659 | * Example of valid types: 6 hours, 2 days, 1 minute. |
---|
660 | * @param mixed $dateString the datestring or unix timestamp to compare |
---|
661 | * @param int $userOffset User's offset from GMT (in hours) |
---|
662 | * @return bool |
---|
663 | * @access public |
---|
664 | * @link http://book.cakephp.org/view/1472/Testing-Time |
---|
665 | */ |
---|
666 | function wasWithinLast($timeInterval, $dateString, $userOffset = null) { |
---|
667 | $tmp = str_replace(' ', '', $timeInterval); |
---|
668 | if (is_numeric($tmp)) { |
---|
669 | $timeInterval = $tmp . ' ' . __('days', true); |
---|
670 | } |
---|
671 | |
---|
672 | $date = $this->fromString($dateString, $userOffset); |
---|
673 | $interval = $this->fromString('-'.$timeInterval); |
---|
674 | |
---|
675 | if ($date >= $interval && $date <= time()) { |
---|
676 | return true; |
---|
677 | } |
---|
678 | |
---|
679 | return false; |
---|
680 | } |
---|
681 | |
---|
682 | /** |
---|
683 | * Returns gmt, given either a UNIX timestamp or a valid strtotime() date string. |
---|
684 | * |
---|
685 | * @param string $dateString Datetime string |
---|
686 | * @return string Formatted date string |
---|
687 | * @access public |
---|
688 | * @link http://book.cakephp.org/view/1471/Formatting |
---|
689 | */ |
---|
690 | function gmt($string = null) { |
---|
691 | if ($string != null) { |
---|
692 | $string = $this->fromString($string); |
---|
693 | } else { |
---|
694 | $string = time(); |
---|
695 | } |
---|
696 | $string = $this->fromString($string); |
---|
697 | $hour = intval(date("G", $string)); |
---|
698 | $minute = intval(date("i", $string)); |
---|
699 | $second = intval(date("s", $string)); |
---|
700 | $month = intval(date("n", $string)); |
---|
701 | $day = intval(date("j", $string)); |
---|
702 | $year = intval(date("Y", $string)); |
---|
703 | |
---|
704 | return gmmktime($hour, $minute, $second, $month, $day, $year); |
---|
705 | } |
---|
706 | |
---|
707 | /** |
---|
708 | * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string. |
---|
709 | * This function also accepts a time string and a format string as first and second parameters. |
---|
710 | * In that case this function behaves as a wrapper for TimeHelper::i18nFormat() |
---|
711 | * |
---|
712 | * @param string $format date format string (or a DateTime string) |
---|
713 | * @param string $dateString Datetime string (or a date format string) |
---|
714 | * @param boolean $invalid flag to ignore results of fromString == false |
---|
715 | * @param int $userOffset User's offset from GMT (in hours) |
---|
716 | * @return string Formatted date string |
---|
717 | * @access public |
---|
718 | */ |
---|
719 | function format($format, $date = null, $invalid = false, $userOffset = null) { |
---|
720 | $time = $this->fromString($date, $userOffset); |
---|
721 | $_time = $this->fromString($format, $userOffset); |
---|
722 | |
---|
723 | if (is_numeric($_time) && $time === false) { |
---|
724 | $format = $date; |
---|
725 | return $this->i18nFormat($_time, $format, $invalid, $userOffset); |
---|
726 | } |
---|
727 | if ($time === false && $invalid !== false) { |
---|
728 | return $invalid; |
---|
729 | } |
---|
730 | return date($format, $time); |
---|
731 | } |
---|
732 | |
---|
733 | /** |
---|
734 | * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string. |
---|
735 | * It take in account the default date format for the current language if a LC_TIME file is used. |
---|
736 | * |
---|
737 | * @param string $dateString Datetime string |
---|
738 | * @param string $format strftime format string. |
---|
739 | * @param boolean $invalid flag to ignore results of fromString == false |
---|
740 | * @param int $userOffset User's offset from GMT (in hours) |
---|
741 | * @return string Formatted and translated date string @access public |
---|
742 | * @access public |
---|
743 | */ |
---|
744 | function i18nFormat($date, $format = null, $invalid = false, $userOffset = null) { |
---|
745 | $date = $this->fromString($date, $userOffset); |
---|
746 | if ($date === false && $invalid !== false) { |
---|
747 | return $invalid; |
---|
748 | } |
---|
749 | if (empty($format)) { |
---|
750 | $format = '%x'; |
---|
751 | } |
---|
752 | $format = $this->convertSpecifiers($format, $date); |
---|
753 | return strftime($format, $date); |
---|
754 | } |
---|
755 | } |
---|