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