Mercurial > hg > Members > shoshi > webvirt
comparison cake/libs/view/helpers/time.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
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-2010, 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-2010, 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 return date("r", $date); | |
444 } | |
445 | |
446 /** | |
447 * Returns either a relative date or a formatted date depending | |
448 * on the difference between the current time and given datetime. | |
449 * $datetime should be in a <i>strtotime</i> - parsable format, like MySQL's datetime datatype. | |
450 * | |
451 * ### Options: | |
452 * | |
453 * - `format` => a fall back format if the relative time is longer than the duration specified by end | |
454 * - `end` => The end of relative time telling | |
455 * - `userOffset` => Users offset from GMT (in hours) | |
456 * | |
457 * Relative dates look something like this: | |
458 * 3 weeks, 4 days ago | |
459 * 15 seconds ago | |
460 * | |
461 * Default date formatting is d/m/yy e.g: on 18/2/09 | |
462 * | |
463 * The returned string includes 'ago' or 'on' and assumes you'll properly add a word | |
464 * like 'Posted ' before the function output. | |
465 * | |
466 * @param string $dateString Datetime string or Unix timestamp | |
467 * @param array $options Default format if timestamp is used in $dateString | |
468 * @return string Relative time string. | |
469 * @access public | |
470 * @link http://book.cakephp.org/view/1471/Formatting | |
471 */ | |
472 function timeAgoInWords($dateTime, $options = array()) { | |
473 $userOffset = null; | |
474 if (is_array($options) && isset($options['userOffset'])) { | |
475 $userOffset = $options['userOffset']; | |
476 } | |
477 $now = time(); | |
478 if (!is_null($userOffset)) { | |
479 $now = $this->convert(time(), $userOffset); | |
480 } | |
481 $inSeconds = $this->fromString($dateTime, $userOffset); | |
482 $backwards = ($inSeconds > $now); | |
483 | |
484 $format = 'j/n/y'; | |
485 $end = '+1 month'; | |
486 | |
487 if (is_array($options)) { | |
488 if (isset($options['format'])) { | |
489 $format = $options['format']; | |
490 unset($options['format']); | |
491 } | |
492 if (isset($options['end'])) { | |
493 $end = $options['end']; | |
494 unset($options['end']); | |
495 } | |
496 } else { | |
497 $format = $options; | |
498 } | |
499 | |
500 if ($backwards) { | |
501 $futureTime = $inSeconds; | |
502 $pastTime = $now; | |
503 } else { | |
504 $futureTime = $now; | |
505 $pastTime = $inSeconds; | |
506 } | |
507 $diff = $futureTime - $pastTime; | |
508 | |
509 // If more than a week, then take into account the length of months | |
510 if ($diff >= 604800) { | |
511 $current = array(); | |
512 $date = array(); | |
513 | |
514 list($future['H'], $future['i'], $future['s'], $future['d'], $future['m'], $future['Y']) = explode('/', date('H/i/s/d/m/Y', $futureTime)); | |
515 | |
516 list($past['H'], $past['i'], $past['s'], $past['d'], $past['m'], $past['Y']) = explode('/', date('H/i/s/d/m/Y', $pastTime)); | |
517 $years = $months = $weeks = $days = $hours = $minutes = $seconds = 0; | |
518 | |
519 if ($future['Y'] == $past['Y'] && $future['m'] == $past['m']) { | |
520 $months = 0; | |
521 $years = 0; | |
522 } else { | |
523 if ($future['Y'] == $past['Y']) { | |
524 $months = $future['m'] - $past['m']; | |
525 } else { | |
526 $years = $future['Y'] - $past['Y']; | |
527 $months = $future['m'] + ((12 * $years) - $past['m']); | |
528 | |
529 if ($months >= 12) { | |
530 $years = floor($months / 12); | |
531 $months = $months - ($years * 12); | |
532 } | |
533 | |
534 if ($future['m'] < $past['m'] && $future['Y'] - $past['Y'] == 1) { | |
535 $years --; | |
536 } | |
537 } | |
538 } | |
539 | |
540 if ($future['d'] >= $past['d']) { | |
541 $days = $future['d'] - $past['d']; | |
542 } else { | |
543 $daysInPastMonth = date('t', $pastTime); | |
544 $daysInFutureMonth = date('t', mktime(0, 0, 0, $future['m'] - 1, 1, $future['Y'])); | |
545 | |
546 if (!$backwards) { | |
547 $days = ($daysInPastMonth - $past['d']) + $future['d']; | |
548 } else { | |
549 $days = ($daysInFutureMonth - $past['d']) + $future['d']; | |
550 } | |
551 | |
552 if ($future['m'] != $past['m']) { | |
553 $months --; | |
554 } | |
555 } | |
556 | |
557 if ($months == 0 && $years >= 1 && $diff < ($years * 31536000)) { | |
558 $months = 11; | |
559 $years --; | |
560 } | |
561 | |
562 if ($months >= 12) { | |
563 $years = $years + 1; | |
564 $months = $months - 12; | |
565 } | |
566 | |
567 if ($days >= 7) { | |
568 $weeks = floor($days / 7); | |
569 $days = $days - ($weeks * 7); | |
570 } | |
571 } else { | |
572 $years = $months = $weeks = 0; | |
573 $days = floor($diff / 86400); | |
574 | |
575 $diff = $diff - ($days * 86400); | |
576 | |
577 $hours = floor($diff / 3600); | |
578 $diff = $diff - ($hours * 3600); | |
579 | |
580 $minutes = floor($diff / 60); | |
581 $diff = $diff - ($minutes * 60); | |
582 $seconds = $diff; | |
583 } | |
584 $relativeDate = ''; | |
585 $diff = $futureTime - $pastTime; | |
586 | |
587 if ($diff > abs($now - $this->fromString($end))) { | |
588 $relativeDate = sprintf(__('on %s',true), date($format, $inSeconds)); | |
589 } else { | |
590 if ($years > 0) { | |
591 // years and months and days | |
592 $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d year', '%d years', $years, true), $years); | |
593 $relativeDate .= $months > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d month', '%d months', $months, true), $months) : ''; | |
594 $relativeDate .= $weeks > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d week', '%d weeks', $weeks, true), $weeks) : ''; | |
595 $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d day', '%d days', $days, true), $days) : ''; | |
596 } elseif (abs($months) > 0) { | |
597 // months, weeks and days | |
598 $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d month', '%d months', $months, true), $months); | |
599 $relativeDate .= $weeks > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d week', '%d weeks', $weeks, true), $weeks) : ''; | |
600 $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d day', '%d days', $days, true), $days) : ''; | |
601 } elseif (abs($weeks) > 0) { | |
602 // weeks and days | |
603 $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d week', '%d weeks', $weeks, true), $weeks); | |
604 $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d day', '%d days', $days, true), $days) : ''; | |
605 } elseif (abs($days) > 0) { | |
606 // days and hours | |
607 $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d day', '%d days', $days, true), $days); | |
608 $relativeDate .= $hours > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d hour', '%d hours', $hours, true), $hours) : ''; | |
609 } elseif (abs($hours) > 0) { | |
610 // hours and minutes | |
611 $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d hour', '%d hours', $hours, true), $hours); | |
612 $relativeDate .= $minutes > 0 ? ($relativeDate ? ', ' : '') . sprintf(__n('%d minute', '%d minutes', $minutes, true), $minutes) : ''; | |
613 } elseif (abs($minutes) > 0) { | |
614 // minutes only | |
615 $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d minute', '%d minutes', $minutes, true), $minutes); | |
616 } else { | |
617 // seconds only | |
618 $relativeDate .= ($relativeDate ? ', ' : '') . sprintf(__n('%d second', '%d seconds', $seconds, true), $seconds); | |
619 } | |
620 | |
621 if (!$backwards) { | |
622 $relativeDate = sprintf(__('%s ago', true), $relativeDate); | |
623 } | |
624 } | |
625 return $relativeDate; | |
626 } | |
627 | |
628 /** | |
629 * Alias for timeAgoInWords | |
630 * | |
631 * @param mixed $dateTime Datetime string (strtotime-compatible) or Unix timestamp | |
632 * @param mixed $options Default format string, if timestamp is used in $dateTime, or an array of options to be passed | |
633 * on to timeAgoInWords(). | |
634 * @return string Relative time string. | |
635 * @see TimeHelper::timeAgoInWords | |
636 * @access public | |
637 * @deprecated This method alias will be removed in future versions. | |
638 * @link http://book.cakephp.org/view/1471/Formatting | |
639 */ | |
640 function relativeTime($dateTime, $options = array()) { | |
641 return $this->timeAgoInWords($dateTime, $options); | |
642 } | |
643 | |
644 /** | |
645 * Returns true if specified datetime was within the interval specified, else false. | |
646 * | |
647 * @param mixed $timeInterval the numeric value with space then time type. | |
648 * Example of valid types: 6 hours, 2 days, 1 minute. | |
649 * @param mixed $dateString the datestring or unix timestamp to compare | |
650 * @param int $userOffset User's offset from GMT (in hours) | |
651 * @return bool | |
652 * @access public | |
653 * @link http://book.cakephp.org/view/1472/Testing-Time | |
654 */ | |
655 function wasWithinLast($timeInterval, $dateString, $userOffset = null) { | |
656 $tmp = str_replace(' ', '', $timeInterval); | |
657 if (is_numeric($tmp)) { | |
658 $timeInterval = $tmp . ' ' . __('days', true); | |
659 } | |
660 | |
661 $date = $this->fromString($dateString, $userOffset); | |
662 $interval = $this->fromString('-'.$timeInterval); | |
663 | |
664 if ($date >= $interval && $date <= time()) { | |
665 return true; | |
666 } | |
667 | |
668 return false; | |
669 } | |
670 | |
671 /** | |
672 * Returns gmt, given either a UNIX timestamp or a valid strtotime() date string. | |
673 * | |
674 * @param string $dateString Datetime string | |
675 * @return string Formatted date string | |
676 * @access public | |
677 * @link http://book.cakephp.org/view/1471/Formatting | |
678 */ | |
679 function gmt($string = null) { | |
680 if ($string != null) { | |
681 $string = $this->fromString($string); | |
682 } else { | |
683 $string = time(); | |
684 } | |
685 $string = $this->fromString($string); | |
686 $hour = intval(date("G", $string)); | |
687 $minute = intval(date("i", $string)); | |
688 $second = intval(date("s", $string)); | |
689 $month = intval(date("n", $string)); | |
690 $day = intval(date("j", $string)); | |
691 $year = intval(date("Y", $string)); | |
692 | |
693 return gmmktime($hour, $minute, $second, $month, $day, $year); | |
694 } | |
695 | |
696 /** | |
697 * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string. | |
698 * This function also accepts a time string and a format string as first and second parameters. | |
699 * In that case this function behaves as a wrapper for TimeHelper::i18nFormat() | |
700 * | |
701 * @param string $format date format string (or a DateTime string) | |
702 * @param string $dateString Datetime string (or a date format string) | |
703 * @param boolean $invalid flag to ignore results of fromString == false | |
704 * @param int $userOffset User's offset from GMT (in hours) | |
705 * @return string Formatted date string | |
706 * @access public | |
707 */ | |
708 function format($format, $date = null, $invalid = false, $userOffset = null) { | |
709 $time = $this->fromString($date, $userOffset); | |
710 $_time = $this->fromString($format, $userOffset); | |
711 | |
712 if (is_numeric($_time) && $time === false) { | |
713 $format = $date; | |
714 return $this->i18nFormat($_time, $format, $invalid, $userOffset); | |
715 } | |
716 if ($time === false && $invalid !== false) { | |
717 return $invalid; | |
718 } | |
719 return date($format, $time); | |
720 } | |
721 | |
722 /** | |
723 * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string. | |
724 * It take in account the default date format for the current language if a LC_TIME file is used. | |
725 * | |
726 * @param string $dateString Datetime string | |
727 * @param string $format strftime format string. | |
728 * @param boolean $invalid flag to ignore results of fromString == false | |
729 * @param int $userOffset User's offset from GMT (in hours) | |
730 * @return string Formatted and translated date string @access public | |
731 * @access public | |
732 */ | |
733 function i18nFormat($date, $format = null, $invalid = false, $userOffset = null) { | |
734 $date = $this->fromString($date, $userOffset); | |
735 if ($date === false && $invalid !== false) { | |
736 return $invalid; | |
737 } | |
738 if (empty($format)) { | |
739 $format = '%x'; | |
740 } | |
741 $format = $this->convertSpecifiers($format, $date); | |
742 return strftime($format, $date); | |
743 } | |
744 } |