~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/libs/view/helpers/time.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

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