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