1: <?php
2: /**
3: * Time Helper 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.View.Helper
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('CakeTime', 'Utility');
21: App::uses('Multibyte', 'I18n');
22: App::uses('AppHelper', 'View/Helper');
23:
24: /**
25: * Time Helper class for easy use of time data.
26: *
27: * Manipulation of time data.
28: *
29: * @package Cake.View.Helper
30: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html
31: * @see CakeTime
32: */
33: class TimeHelper extends AppHelper {
34:
35: /**
36: * CakeTime instance
37: */
38: protected $_engine = null;
39:
40: /**
41: * Constructor
42: *
43: * ### Settings:
44: *
45: * - `engine` Class name to use to replace CakeTime functionality
46: * The class needs to be placed in the `Utility` directory.
47: *
48: * @param View $View the view object the helper is attached to.
49: * @param array $settings Settings array Settings array
50: * @throws CakeException When the engine class could not be found.
51: */
52: public function __construct(View $View, $settings = array()) {
53: $settings = Hash::merge(array('engine' => 'CakeTime'), $settings);
54: parent::__construct($View, $settings);
55: list($plugin, $engineClass) = pluginSplit($settings['engine'], true);
56: App::uses($engineClass, $plugin . 'Utility');
57: if (class_exists($engineClass)) {
58: $this->_engine = new $engineClass($settings);
59: } else {
60: throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
61: }
62: }
63:
64: /**
65: * Magic accessor for deprecated attributes.
66: *
67: * @param string $name Name of the attribute to set.
68: * @param string $value Value of the attribute to set.
69: * @return mixed
70: */
71: public function __set($name, $value) {
72: switch ($name) {
73: case 'niceFormat':
74: $this->_engine->{$name} = $value;
75: break;
76: default:
77: $this->{$name} = $value;
78: break;
79: }
80: }
81:
82: /**
83: * Magic isset check for deprecated attributes.
84: *
85: * @param string $name Name of the attribute to check.
86: * @return boolean
87: */
88: public function __isset($name) {
89: if (isset($this->{$name})) {
90: return true;
91: }
92: $magicGet = array('niceFormat');
93: if (in_array($name, $magicGet)) {
94: return $this->__get($name) !== null;
95: }
96: return null;
97: }
98:
99: /**
100: * Magic accessor for attributes that were deprecated.
101: *
102: * @param string $name Name of the attribute to get.
103: * @return mixed
104: */
105: public function __get($name) {
106: if (isset($this->_engine->{$name})) {
107: return $this->_engine->{$name};
108: }
109: $magicGet = array('niceFormat');
110: if (in_array($name, $magicGet)) {
111: return $this->_engine->{$name};
112: }
113: return null;
114: }
115:
116: /**
117: * Call methods from CakeTime utility class
118: */
119: public function __call($method, $params) {
120: return call_user_func_array(array($this->_engine, $method), $params);
121: }
122:
123: /**
124: * @see CakeTime::convertSpecifiers()
125: *
126: * @param string $format Format with specifiers for strftime function.
127: * Accepts the special specifier %S which mimics the modifier S for date()
128: * @param string $time UNIX timestamp
129: * @return string windows safe and date() function compatible format for strftime
130: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
131: */
132: public function convertSpecifiers($format, $time = null) {
133: return $this->_engine->convertSpecifiers($format, $time);
134: }
135:
136: /**
137: * @see CakeTime::convert()
138: *
139: * @param string $serverTime UNIX timestamp
140: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
141: * @return integer UNIX timestamp
142: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
143: */
144: public function convert($serverTime, $timezone) {
145: return $this->_engine->convert($serverTime, $timezone);
146: }
147:
148: /**
149: * @see CakeTime::serverOffset()
150: *
151: * @return integer Offset
152: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
153: */
154: public function serverOffset() {
155: return $this->_engine->serverOffset();
156: }
157:
158: /**
159: * @see CakeTime::fromString()
160: *
161: * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
162: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
163: * @return string Parsed timestamp
164: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
165: */
166: public function fromString($dateString, $timezone = null) {
167: return $this->_engine->fromString($dateString, $timezone);
168: }
169:
170: /**
171: * @see CakeTime::nice()
172: *
173: * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
174: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
175: * @param string $format The format to use. If null, `TimeHelper::$niceFormat` is used
176: * @return string Formatted date string
177: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
178: */
179: public function nice($dateString = null, $timezone = null, $format = null) {
180: return $this->_engine->nice($dateString, $timezone, $format);
181: }
182:
183: /**
184: * @see CakeTime::niceShort()
185: *
186: * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime objectp
187: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
188: * @return string Described, relative date string
189: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
190: */
191: public function niceShort($dateString = null, $timezone = null) {
192: return $this->_engine->niceShort($dateString, $timezone);
193: }
194:
195: /**
196: * @see CakeTime::daysAsSql()
197: *
198: * @param integer|string|DateTime $begin UNIX timestamp, strtotime() valid string or DateTime object
199: * @param integer|string|DateTime $end UNIX timestamp, strtotime() valid string or DateTime object
200: * @param string $fieldName Name of database field to compare with
201: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
202: * @return string Partial SQL string.
203: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
204: */
205: public function daysAsSql($begin, $end, $fieldName, $timezone = null) {
206: return $this->_engine->daysAsSql($begin, $end, $fieldName, $timezone);
207: }
208:
209: /**
210: * @see CakeTime::dayAsSql()
211: *
212: * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
213: * @param string $fieldName Name of database field to compare with
214: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
215: * @return string Partial SQL string.
216: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
217: */
218: public function dayAsSql($dateString, $fieldName, $timezone = null) {
219: return $this->_engine->dayAsSql($dateString, $fieldName, $timezone);
220: }
221:
222: /**
223: * @see CakeTime::isToday()
224: *
225: * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
226: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
227: * @return boolean True if datetime string is today
228: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
229: */
230: public function isToday($dateString, $timezone = null) {
231: return $this->_engine->isToday($dateString, $timezone);
232: }
233:
234: /**
235: * @see CakeTime::isThisWeek()
236: *
237: * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
238: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
239: * @return boolean True if datetime string is within current week
240: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
241: */
242: public function isThisWeek($dateString, $timezone = null) {
243: return $this->_engine->isThisWeek($dateString, $timezone);
244: }
245:
246: /**
247: * @see CakeTime::isThisMonth()
248: *
249: * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
250: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
251: * @return boolean True if datetime string is within current month
252: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
253: */
254: public function isThisMonth($dateString, $timezone = null) {
255: return $this->_engine->isThisMonth($dateString, $timezone);
256: }
257:
258: /**
259: * @see CakeTime::isThisYear()
260: *
261: * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
262: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
263: * @return boolean True if datetime string is within current year
264: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
265: */
266: public function isThisYear($dateString, $timezone = null) {
267: return $this->_engine->isThisYear($dateString, $timezone);
268: }
269:
270: /**
271: * @see CakeTime::wasYesterday()
272: *
273: * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
274: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
275: * @return boolean True if datetime string was yesterday
276: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
277: *
278: */
279: public function wasYesterday($dateString, $timezone = null) {
280: return $this->_engine->wasYesterday($dateString, $timezone);
281: }
282:
283: /**
284: * @see CakeTime::isTomorrow()
285: *
286: * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
287: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
288: * @return boolean True if datetime string was yesterday
289: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
290: */
291: public function isTomorrow($dateString, $timezone = null) {
292: return $this->_engine->isTomorrow($dateString, $timezone);
293: }
294:
295: /**
296: * @see CakeTime::toQuarter()
297: *
298: * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
299: * @param boolean $range if true returns a range in Y-m-d format
300: * @return mixed 1, 2, 3, or 4 quarter of year or array if $range true
301: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
302: */
303: public function toQuarter($dateString, $range = false) {
304: return $this->_engine->toQuarter($dateString, $range);
305: }
306:
307: /**
308: * @see CakeTime::toUnix()
309: *
310: * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
311: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
312: * @return integer Unix timestamp
313: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
314: */
315: public function toUnix($dateString, $timezone = null) {
316: return $this->_engine->toUnix($dateString, $timezone);
317: }
318:
319: /**
320: * @see CakeTime::toAtom()
321: *
322: * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
323: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
324: * @return string Formatted date string
325: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
326: */
327: public function toAtom($dateString, $timezone = null) {
328: return $this->_engine->toAtom($dateString, $timezone);
329: }
330:
331: /**
332: * @see CakeTime::toRSS()
333: *
334: * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
335: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
336: * @return string Formatted date string
337: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
338: */
339: public function toRSS($dateString, $timezone = null) {
340: return $this->_engine->toRSS($dateString, $timezone);
341: }
342:
343: /**
344: * @see CakeTime::timeAgoInWords()
345: *
346: * ## Addition options
347: *
348: * - `element` - The element to wrap the formatted time in.
349: * Has a few additional options:
350: * - `tag` - The tag to use, defaults to 'span'.
351: * - `class` - The classname to use, defaults to `time-ago-in-words`.
352: * - `title` - Defaults to the $dateTime input.
353: *
354: * @param integer|string|DateTime $dateTime UNIX timestamp, strtotime() valid string or DateTime object
355: * @param array $options Default format if timestamp is used in $dateString
356: * @return string Relative time string.
357: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
358: */
359: public function timeAgoInWords($dateTime, $options = array()) {
360: $element = null;
361:
362: if (is_array($options) && !empty($options['element'])) {
363: $element = array(
364: 'tag' => 'span',
365: 'class' => 'time-ago-in-words',
366: 'title' => $dateTime
367: );
368:
369: if (is_array($options['element'])) {
370: $element = array_merge($element, $options['element']);
371: } else {
372: $element['tag'] = $options['element'];
373: }
374: unset($options['element']);
375: }
376: $relativeDate = $this->_engine->timeAgoInWords($dateTime, $options);
377:
378: if ($element) {
379: $relativeDate = sprintf(
380: '<%s%s>%s</%s>',
381: $element['tag'],
382: $this->_parseAttributes($element, array('tag')),
383: $relativeDate,
384: $element['tag']
385: );
386: }
387: return $relativeDate;
388: }
389:
390: /**
391: * @see CakeTime::wasWithinLast()
392: *
393: * @param string|integer $timeInterval the numeric value with space then time type.
394: * Example of valid types: 6 hours, 2 days, 1 minute.
395: * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
396: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
397: * @return boolean
398: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
399: */
400: public function wasWithinLast($timeInterval, $dateString, $timezone = null) {
401: return $this->_engine->wasWithinLast($timeInterval, $dateString, $timezone);
402: }
403:
404: /**
405: * @see CakeTime::isWithinLast()
406: *
407: * @param string|integer $timeInterval the numeric value with space then time type.
408: * Example of valid types: 6 hours, 2 days, 1 minute.
409: * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
410: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
411: * @return boolean
412: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
413: */
414: public function isWithinNext($timeInterval, $dateString, $timezone = null) {
415: return $this->_engine->isWithinNext($timeInterval, $dateString, $timezone);
416: }
417:
418: /**
419: * @see CakeTime::gmt()
420: *
421: * @param integer|string|DateTime $string UNIX timestamp, strtotime() valid string or DateTime object
422: * @return integer UNIX timestamp
423: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
424: */
425: public function gmt($string = null) {
426: return $this->_engine->gmt($string);
427: }
428:
429: /**
430: * @see CakeTime::format()
431: *
432: * @param integer|string|DateTime $format date format string (or a UNIX timestamp, strtotime() valid string or DateTime object)
433: * @param integer|string|DateTime $date UNIX timestamp, strtotime() valid string or DateTime object (or a date format string)
434: * @param boolean $invalid flag to ignore results of fromString == false
435: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
436: * @return string Formatted date string
437: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
438: */
439: public function format($format, $date = null, $invalid = false, $timezone = null) {
440: return $this->_engine->format($format, $date, $invalid, $timezone);
441: }
442:
443: /**
444: * @see CakeTime::i18nFormat()
445: *
446: * @param integer|string|DateTime $date UNIX timestamp, strtotime() valid string or DateTime object
447: * @param string $format strftime format string.
448: * @param boolean $invalid flag to ignore results of fromString == false
449: * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
450: * @return string Formatted and translated date string
451: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
452: */
453: public function i18nFormat($date, $format = null, $invalid = false, $timezone = null) {
454: return $this->_engine->i18nFormat($date, $format, $invalid, $timezone);
455: }
456:
457: }
458: