CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.1 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.1
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

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

Generated using CakePHP API Docs