1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23:
24: 25: 26: 27: 28: 29: 30: 31:
32: class TimeHelper extends AppHelper {
33: 34: 35: 36: 37: 38: 39:
40: function convert($serverTime, $userOffset) {
41: $serverOffset = $this->serverOffset();
42: $gmtTime = $serverTime - $serverOffset;
43: $userTime = $gmtTime + $userOffset * (60*60);
44: return $userTime;
45: }
46: 47: 48: 49: 50:
51: function serverOffset() {
52: return date('Z', time());
53: }
54: 55: 56: 57: 58: 59: 60:
61: function fromString($dateString, $userOffset = null) {
62: if (empty($dateString)) {
63: return false;
64: }
65: if (is_int($dateString) || is_numeric($dateString)) {
66: $date = intval($dateString);
67: } else {
68: $date = strtotime($dateString);
69: }
70: if ($userOffset !== null) {
71: return $this->convert($date, $userOffset);
72: }
73: return $date;
74: }
75: 76: 77: 78: 79: 80: 81:
82: function nice($dateString = null, $userOffset = null) {
83: if ($dateString != null) {
84: $date = $this->fromString($dateString, $userOffset);
85: } else {
86: $date = time();
87: }
88:
89: $ret = date("D, M jS Y, H:i", $date);
90: return $this->output($ret);
91: }
92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103:
104: function niceShort($dateString = null, $userOffset = null) {
105: $date = $dateString ? $this->fromString($dateString, $userOffset) : time();
106:
107: $y = $this->isThisYear($date) ? '' : ' Y';
108:
109: if ($this->isToday($dateString, $userOffset)) {
110: $ret = sprintf(__('Today, %s',true), date("H:i", $date));
111: } elseif ($this->wasYesterday($dateString, $userOffset)) {
112: $ret = sprintf(__('Yesterday, %s',true), date("H:i", $date));
113: } else {
114: $ret = date("M jS{$y}, H:i", $date);
115: }
116:
117: return $this->output($ret);
118: }
119: 120: 121: 122: 123: 124: 125: 126: 127:
128: function daysAsSql($begin, $end, $fieldName, $userOffset = null) {
129: $begin = $this->fromString($begin, $userOffset);
130: $end = $this->fromString($end, $userOffset);
131: $begin = date('Y-m-d', $begin) . ' 00:00:00';
132: $end = date('Y-m-d', $end) . ' 23:59:59';
133:
134: $ret ="($fieldName >= '$begin') AND ($fieldName <= '$end')";
135: return $this->output($ret);
136: }
137: 138: 139: 140: 141: 142: 143: 144: 145:
146: function dayAsSql($dateString, $fieldName, $userOffset = null) {
147: $date = $this->fromString($dateString, $userOffset);
148: $ret = $this->daysAsSql($dateString, $dateString, $fieldName);
149: return $this->output($ret);
150: }
151: 152: 153: 154: 155: 156: 157:
158: function isToday($dateString, $userOffset = null) {
159: $date = $this->fromString($dateString, $userOffset);
160: return date('Y-m-d', $date) == date('Y-m-d', time());
161: }
162: 163: 164: 165: 166: 167:
168: function isThisWeek($dateString, $userOffset = null) {
169: $date = $this->fromString($dateString, $userOffset);
170: return date('W Y', $date) == date('W Y', time());
171: }
172: 173: 174: 175: 176: 177:
178: function isThisMonth($dateString, $userOffset = null) {
179: $date = $this->fromString($dateString);
180: return date('m Y',$date) == date('m Y', time());
181: }
182: 183: 184: 185: 186: 187:
188: function isThisYear($dateString, $userOffset = null) {
189: $date = $this->fromString($dateString, $userOffset);
190: return date('Y', $date) == date('Y', time());
191: }
192: 193: 194: 195: 196: 197: 198:
199: function wasYesterday($dateString, $userOffset = null) {
200: $date = $this->fromString($dateString, $userOffset);
201: return date('Y-m-d', $date) == date('Y-m-d', strtotime('yesterday'));
202: }
203: 204: 205: 206: 207: 208: 209:
210: function isTomorrow($dateString, $userOffset = null) {
211: $date = $this->fromString($dateString, $userOffset);
212: return date('Y-m-d', $date) == date('Y-m-d', strtotime('tomorrow'));
213: }
214: 215: 216: 217: 218: 219:
220: function toQuarter($dateString, $range = false) {
221: $time = $this->fromString($dateString);
222: $date = ceil(date('m', $time) / 3);
223:
224: if ($range === true) {
225: $range = 'Y-m-d';
226: }
227:
228: if ($range !== false) {
229: $year = date('Y', $time);
230:
231: switch ($date) {
232: case 1:
233: $date = array($year.'-01-01', $year.'-03-31');
234: break;
235: case 2:
236: $date = array($year.'-04-01', $year.'-06-30');
237: break;
238: case 3:
239: $date = array($year.'-07-01', $year.'-09-30');
240: break;
241: case 4:
242: $date = array($year.'-10-01', $year.'-12-31');
243: break;
244: }
245: }
246: return $this->output($date);
247: }
248: 249: 250: 251: 252: 253: 254:
255: function toUnix($dateString, $userOffset = null) {
256: $ret = $this->fromString($dateString, $userOffset);
257: return $this->output($ret);
258: }
259: 260: 261: 262: 263: 264: 265:
266: function toAtom($dateString, $userOffset = null) {
267: $date = $this->fromString($dateString, $userOffset);
268: $ret = date('Y-m-d\TH:i:s\Z', $date);
269: return $this->output($ret);
270: }
271: 272: 273: 274: 275: 276: 277:
278: function toRSS($dateString, $userOffset = null) {
279: $date = $this->fromString($dateString, $userOffset);
280: $ret = date("r", $date);
281: return $this->output($ret);
282: }
283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306:
307: function timeAgoInWords($dateTime, $options = array()) {
308: $userOffset = null;
309: if (is_array($options) && isset($options['userOffset'])) {
310: $userOffset = $options['userOffset'];
311: }
312: $now = time();
313: if (!is_null($userOffset)) {
314: $now = $this->convert(time(), $userOffset);
315: }
316: $inSeconds = $this->fromString($dateTime, $userOffset);
317: $backwards = ($inSeconds > $now);
318:
319: $format = 'j/n/y';
320: $end = '+1 month';
321:
322: if (is_array($options)) {
323: if (isset($options['format'])) {
324: $format = $options['format'];
325: unset($options['format']);
326: }
327: if (isset($options['end'])) {
328: $end = $options['end'];
329: unset($options['end']);
330: }
331: } else {
332: $format = $options;
333: }
334:
335: if ($backwards) {
336: $futureTime = $inSeconds;
337: $pastTime = $now;
338: } else {
339: $futureTime = $now;
340: $pastTime = $inSeconds;
341: }
342: $diff = $futureTime - $pastTime;
343:
344:
345: if ($diff >= 604800) {
346: $current = array();
347: $date = array();
348:
349: list($future['H'], $future['i'], $future['s'], $future['d'], $future['m'], $future['Y']) = explode('/', date('H/i/s/d/m/Y', $futureTime));
350:
351: list($past['H'], $past['i'], $past['s'], $past['d'], $past['m'], $past['Y']) = explode('/', date('H/i/s/d/m/Y', $pastTime));
352: $years = $months = $weeks = $days = $hours = $minutes = $seconds = 0;
353:
354: if ($future['Y'] == $past['Y'] && $future['m'] == $past['m']) {
355: $months = 0;
356: $years = 0;
357: } else {
358: if ($future['Y'] == $past['Y']) {
359: $months = $future['m'] - $past['m'];
360: } else {
361: $years = $future['Y'] - $past['Y'];
362: $months = $future['m'] + ((12 * $years) - $past['m']);
363:
364: if ($months >= 12) {
365: $years = floor($months / 12);
366: $months = $months - ($years * 12);
367: }
368:
369: if ($future['m'] < $past['m'] && $future['Y'] - $past['Y'] == 1) {
370: $years --;
371: }
372: }
373: }
374:
375: if ($future['d'] >= $past['d']) {
376: $days = $future['d'] - $past['d'];
377: } else {
378: $daysInPastMonth = date('t', $pastTime);
379: $daysInFutureMonth = date('t', mktime(0, 0, 0, $future['m'] - 1, 1, $future['Y']));
380:
381: if (!$backwards) {
382: $days = ($daysInPastMonth - $past['d']) + $future['d'];
383: } else {
384: $days = ($daysInFutureMonth - $past['d']) + $future['d'];
385: }
386:
387: if ($future['m'] != $past['m']) {
388: $months --;
389: }
390: }
391:
392: if ($months == 0 && $years >= 1 && $diff < ($years * 31536000)) {
393: $months = 11;
394: $years --;
395: }
396:
397: if ($months >= 12) {
398: $years = $years + 1;
399: $months = $months - 12;
400: }
401:
402: if ($days >= 7) {
403: $weeks = floor($days / 7);
404: $days = $days - ($weeks * 7);
405: }
406: } else {
407: $years = $months = $weeks = 0;
408: $days = floor($diff / 86400);
409:
410: $diff = $diff - ($days * 86400);
411:
412: $hours = floor($diff / 3600);
413: $diff = $diff - ($hours * 3600);
414:
415: $minutes = floor($diff / 60);
416: $diff = $diff - ($minutes * 60);
417: $seconds = $diff;
418: }
419: $relativeDate = '';
420: $diff = $futureTime - $pastTime;
421:
422: if ($diff > abs($now - $this->fromString($end))) {
423: $relativeDate = sprintf(__('on %s',true), date($format, $inSeconds));
424: } else {
425: if ($years > 0) {
426:
427: $relativeDate .= ($relativeDate ? ', ' : '') . $years . ' ' . __n('year', 'years', $years, true);
428: $relativeDate .= $months > 0 ? ($relativeDate ? ', ' : '') . $months . ' ' . __n('month', 'months', $months, true) : '';
429: $relativeDate .= $weeks > 0 ? ($relativeDate ? ', ' : '') . $weeks . ' ' . __n('week', 'weeks', $weeks, true) : '';
430: $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . $days . ' ' . __n('day', 'days', $days, true) : '';
431: } elseif (abs($months) > 0) {
432:
433: $relativeDate .= ($relativeDate ? ', ' : '') . $months . ' ' . __n('month', 'months', $months, true);
434: $relativeDate .= $weeks > 0 ? ($relativeDate ? ', ' : '') . $weeks . ' ' . __n('week', 'weeks', $weeks, true) : '';
435: $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . $days . ' ' . __n('day', 'days', $days, true) : '';
436: } elseif (abs($weeks) > 0) {
437:
438: $relativeDate .= ($relativeDate ? ', ' : '') . $weeks . ' ' . __n('week', 'weeks', $weeks, true);
439: $relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . $days . ' ' . __n('day', 'days', $days, true) : '';
440: } elseif (abs($days) > 0) {
441:
442: $relativeDate .= ($relativeDate ? ', ' : '') . $days . ' ' . __n('day', 'days', $days, true);
443: $relativeDate .= $hours > 0 ? ($relativeDate ? ', ' : '') . $hours . ' ' . __n('hour', 'hours', $hours, true) : '';
444: } elseif (abs($hours) > 0) {
445:
446: $relativeDate .= ($relativeDate ? ', ' : '') . $hours . ' ' . __n('hour', 'hours', $hours, true);
447: $relativeDate .= $minutes > 0 ? ($relativeDate ? ', ' : '') . $minutes . ' ' . __n('minute', 'minutes', $minutes, true) : '';
448: } elseif (abs($minutes) > 0) {
449:
450: $relativeDate .= ($relativeDate ? ', ' : '') . $minutes . ' ' . __n('minute', 'minutes', $minutes, true);
451: } else {
452:
453: $relativeDate .= ($relativeDate ? ', ' : '') . $seconds . ' ' . __n('second', 'seconds', $seconds, true);
454: }
455:
456: if (!$backwards) {
457: $relativeDate = sprintf(__('%s ago', true), $relativeDate);
458: }
459: }
460: return $this->output($relativeDate);
461: }
462: 463: 464: 465: 466: 467: 468: 469: 470:
471: function relativeTime($dateTime, $options = array()) {
472: return $this->timeAgoInWords($dateTime, $options);
473: }
474: 475: 476: 477: 478: 479: 480: 481:
482: function wasWithinLast($timeInterval, $dateString, $userOffset = null) {
483: $tmp = str_replace(' ', '', $timeInterval);
484: if (is_numeric($tmp)) {
485: $timeInterval = $tmp . ' ' . __('days', true);
486: }
487:
488: $date = $this->fromString($dateString, $userOffset);
489: $interval = $this->fromString('-'.$timeInterval);
490:
491: if ($date >= $interval && $date <= time()) {
492: return true;
493: }
494:
495: return false;
496: }
497: 498: 499: 500: 501: 502:
503: function gmt($string = null) {
504: if ($string != null) {
505: $string = $this->fromString($string);
506: } else {
507: $string = time();
508: }
509: $string = $this->fromString($string);
510: $hour = intval(date("G", $string));
511: $minute = intval(date("i", $string));
512: $second = intval(date("s", $string));
513: $month = intval(date("n", $string));
514: $day = intval(date("j", $string));
515: $year = intval(date("Y", $string));
516:
517: $return = gmmktime($hour, $minute, $second, $month, $day, $year);
518: return $return;
519: }
520: 521: 522: 523: 524: 525: 526: 527: 528:
529: function format($format = 'd-m-Y', $date, $invalid = false, $userOffset = null) {
530: $date = $this->fromString($date, $userOffset);
531: if ($date === false && $invalid !== false) {
532: return $invalid;
533: }
534: return date($format, $date);
535: }
536: }
537: ?>