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 = Set::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 integer $userOffset User's offset from GMT (in hours)
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, $userOffset) {
145: return $this->_engine->convert($serverTime, $userOffset);
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 string $dateString Datetime string
162: * @param integer $userOffset User's offset from GMT (in hours)
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, $userOffset = null) {
167: return $this->_engine->fromString($dateString, $userOffset);
168: }
169:
170: /**
171: * @see CakeTime::nice()
172: *
173: * @param string $dateString Datetime string or Unix timestamp
174: * @param integer $userOffset User's offset from GMT (in hours)
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, $userOffset = null, $format = null) {
180: return $this->_engine->nice($dateString, $userOffset, $format);
181: }
182:
183: /**
184: * @see CakeTime::niceShort()
185: *
186: * @param string $dateString Datetime string or Unix timestamp
187: * @param integer $userOffset User's offset from GMT (in hours)
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, $userOffset = null) {
192: return $this->_engine->niceShort($dateString, $userOffset);
193: }
194:
195: /**
196: * @see CakeTime::daysAsSql()
197: *
198: * @param string $begin Datetime string or Unix timestamp
199: * @param string $end Datetime string or Unix timestamp
200: * @param string $fieldName Name of database field to compare with
201: * @param integer $userOffset User's offset from GMT (in hours)
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, $userOffset = null) {
206: return $this->_engine->daysAsSql($begin, $end, $fieldName, $userOffset);
207: }
208:
209: /**
210: * @see CakeTime::dayAsSql()
211: *
212: * @param string $dateString Datetime string or Unix timestamp
213: * @param string $fieldName Name of database field to compare with
214: * @param integer $userOffset User's offset from GMT (in hours)
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, $userOffset = null) {
219: return $this->_engine->dayAsSql($dateString, $fieldName, $userOffset);
220: }
221:
222: /**
223: * @see CakeTime::isToday()
224: *
225: * @param string $dateString Datetime string or Unix timestamp
226: * @param integer $userOffset User's offset from GMT (in hours)
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, $userOffset = null) {
231: return $this->_engine->isToday($dateString, $userOffset);
232: }
233:
234: /**
235: * @see CakeTime::isThisWeek()
236: *
237: * @param string $dateString
238: * @param integer $userOffset User's offset from GMT (in hours)
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, $userOffset = null) {
243: return $this->_engine->isThisWeek($dateString, $userOffset);
244: }
245:
246: /**
247: * @see CakeTime::isThisMonth()
248: *
249: * @param string $dateString
250: * @param integer $userOffset User's offset from GMT (in hours)
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, $userOffset = null) {
255: return $this->_engine->isThisMonth($dateString, $userOffset);
256: }
257:
258: /**
259: * @see CakeTime::isThisYear()
260: *
261: * @param string $dateString Datetime string or Unix timestamp
262: * @param integer $userOffset User's offset from GMT (in hours)
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, $userOffset = null) {
267: return $this->_engine->isThisYear($dateString, $userOffset);
268: }
269:
270: /**
271: * @see CakeTime::wasYesterday()
272: *
273: * @param string $dateString Datetime string or Unix timestamp
274: * @param integer $userOffset User's offset from GMT (in hours)
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, $userOffset = null) {
280: return $this->_engine->wasYesterday($dateString, $userOffset);
281: }
282:
283: /**
284: * @see CakeTime::isTomorrow()
285: *
286: * @param string $dateString Datetime string or Unix timestamp
287: * @param integer $userOffset User's offset from GMT (in hours)
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, $userOffset = null) {
292: return $this->_engine->isTomorrow($dateString, $userOffset);
293: }
294:
295: /**
296: * @see CakeTime::toQuarter()
297: *
298: * @param string $dateString
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 string $dateString Datetime string to be represented as a Unix timestamp
311: * @param integer $userOffset User's offset from GMT (in hours)
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, $userOffset = null) {
316: return $this->_engine->toUnix($dateString, $userOffset);
317: }
318:
319: /**
320: * @see CakeTime::toAtom()
321: *
322: * @param string $dateString Datetime string or Unix timestamp
323: * @param integer $userOffset User's offset from GMT (in hours)
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, $userOffset = null) {
328: return $this->_engine->toAtom($dateString, $userOffset);
329: }
330:
331: /**
332: * @see CakeTime::toRSS()
333: *
334: * @param string $dateString Datetime string or Unix timestamp
335: * @param integer $userOffset User's offset from GMT (in hours)
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, $userOffset = null) {
340: return $this->_engine->toRSS($dateString, $userOffset);
341: }
342:
343: /**
344: * @see CakeTime::timeAgoInWords()
345: *
346: * @param string $dateTime Datetime string or Unix timestamp
347: * @param array $options Default format if timestamp is used in $dateString
348: * @return string Relative time string.
349: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
350: */
351: public function timeAgoInWords($dateTime, $options = array()) {
352: return $this->_engine->timeAgoInWords($dateTime, $options);
353: }
354:
355: /**
356: * @see CakeTime::wasWithinLast()
357: *
358: * @param mixed $timeInterval the numeric value with space then time type.
359: * Example of valid types: 6 hours, 2 days, 1 minute.
360: * @param mixed $dateString the datestring or unix timestamp to compare
361: * @param integer $userOffset User's offset from GMT (in hours)
362: * @return boolean
363: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
364: */
365: public function wasWithinLast($timeInterval, $dateString, $userOffset = null) {
366: return $this->_engine->wasWithinLast($timeInterval, $dateString, $userOffset);
367: }
368:
369: /**
370: * @see CakeTime::gmt()
371: *
372: * @param string $string UNIX timestamp or a valid strtotime() date string
373: * @return integer UNIX timestamp
374: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
375: */
376: public function gmt($string = null) {
377: return $this->_engine->gmt($string);
378: }
379:
380: /**
381: * @see CakeTime::format()
382: *
383: * @param string $format date format string (or a DateTime string)
384: * @param string $date Datetime string (or a date format string)
385: * @param boolean $invalid flag to ignore results of fromString == false
386: * @param integer $userOffset User's offset from GMT (in hours)
387: * @return string Formatted date string
388: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
389: */
390: public function format($format, $date = null, $invalid = false, $userOffset = null) {
391: return $this->_engine->format($format, $date, $invalid, $userOffset);
392: }
393:
394: /**
395: * @see CakeTime::i18nFormat()
396: *
397: * @param string $date Datetime string
398: * @param string $format strftime format string.
399: * @param boolean $invalid flag to ignore results of fromString == false
400: * @param integer $userOffset User's offset from GMT (in hours)
401: * @return string Formatted and translated date string
402: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
403: */
404: public function i18nFormat($date, $format = null, $invalid = false, $userOffset = null) {
405: return $this->_engine->i18nFormat($date, $format, $invalid, $userOffset);
406: }
407:
408: }
409: