DatetimeHelper.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. App::uses('DatetimeLib', 'Tools.Lib');
  3. App::uses('TimeHelper', 'View/Helper');
  4. class DatetimeHelper extends TimeHelper {
  5. public $helpers = array('Html');
  6. public $Datetime;
  7. protected $userOffset = null;
  8. protected $daylightSavings = false;
  9. public function __construct($View = null, $settings = array()) {
  10. parent::__construct($View, $settings);
  11. $i18n = Configure::read('Localization');
  12. if (!empty($i18n['time_offset'])) {
  13. $this->userOffset = (int)$i18n['time_offset'];
  14. }
  15. if (!empty($i18n['daylight_savings'])) {
  16. $this->daylightSavings = (bool)$i18n['daylight_savings'];
  17. }
  18. $this->Datetime = new DatetimeLib();
  19. }
  20. public function __call($method, $params) {
  21. if (!method_exists($this, 'call__')) {
  22. //trigger_error(__('Magic method handler call__ not defined in %s', get_class($this)), E_USER_ERROR);
  23. }
  24. return call_user_func_array(array($this->Datetime, $method), $params);
  25. }
  26. /**
  27. * EXPERIMENTAL!!!
  28. * @param
  29. * @param
  30. * @return int offset
  31. * 2009-03-19 ms
  32. */
  33. public function tzOffset($gmtoffset, $is_dst) {
  34. //global $gmtoffset, $is_dst;
  35. extract(getdate());
  36. $serveroffset = gmmktime(0,0,0,$mon,$mday,$year) - mktime(0,0,0,$mon,$mday,$year);
  37. $offset = $gmtoffset - $serveroffset;
  38. return $offset + ($is_dst ? 3600 : 0);
  39. }
  40. /**
  41. * @param string date (from db)
  42. * @return int $age on success, mixed $default otherwise
  43. * 2009-11-22 ms
  44. */
  45. public function userAge($date = null, $default = '---') {
  46. if ((int)$date === 0) {
  47. return $default;
  48. }
  49. $age = $this->Datetime->age($date, null);
  50. if ($age >= 1 && $age <= 99) {
  51. return $age;
  52. }
  53. return $default;
  54. }
  55. /**
  56. * Like localDate(), only with additional markup <span> and class="today", if today, etc
  57. * 2009-11-22 ms
  58. */
  59. public function localDateMarkup($dateString = null, $format = null, $options = array()) {
  60. $date = $this->Datetime->localDate($dateString, $format, $options);
  61. $date = '<span'.($this->isToday($dateString,(isset($options['userOffset'])?$options['userOffset']:null))?' class="today"':'').'>'.$date.'</span>';
  62. return $date;
  63. }
  64. /**
  65. * Like niceDate(), only with additional markup <span> and class="today", if today, etc
  66. * 2009-11-22 ms
  67. */
  68. public function niceDateMarkup($dateString = null, $format = null, $options = array()) {
  69. $date = $this->niceDate($dateString, $format, $options);
  70. $date = '<span'.($this->isToday($dateString,(isset($options['userOffset'])?$options['userOffset']:null))?' class="today"':'').'>'.$date.'</span>';
  71. return $date;
  72. }
  73. /**
  74. * returns red/specialGreen/green date depending on the current day
  75. * @param date in DB Format (xxxx-xx-xx)
  76. * ...
  77. * @param array $options
  78. * @param array $attr: html attributes
  79. * @return nicely formatted date
  80. * 2009-07-25 ms
  81. * // TODO refactor!
  82. */
  83. public function published($dateString = null, $userOffset = null, $options=array(), $attr=array()) {
  84. $date = $dateString ? $this->fromString($dateString, $userOffset) : null; // time() ?
  85. $niceDate = '';
  86. $when = null;
  87. $span = '';
  88. $spanEnd = '';
  89. $whenArray = array('-1'=>'already','0'=>'today','1'=>'notyet');
  90. $titles = array('-1'=>__('publishedAlready'),'0'=>__('publishedToday'),'1'=>__('publishedNotYet'));
  91. if (!empty($date)) {
  92. $y = $this->isThisYear($date) ? '' : ' Y';
  93. $format = (!empty($options['format'])?$options['format']:FORMAT_NICE_YMD);
  94. # Hack
  95. # //TODO: get this to work with datetime - somehow cleaner
  96. $timeAttachment = '';
  97. if (isset($options['niceDateTime'])) {
  98. $timeAttachment = ', '.$this->niceDate($date, $options['niceDateTime']);
  99. $whenOverride = true;
  100. }
  101. if ($this->isToday($date)) {
  102. $when = 0;
  103. $niceDate = __('Today').$timeAttachment;
  104. } elseif ($this->isTomorrow($date)) {
  105. $when = 1;
  106. $niceDate = __('Tomorrow').$timeAttachment;
  107. } elseif ($this->wasYesterday($date)) {
  108. $when = -1;
  109. $niceDate = __('Yesterday').$timeAttachment;
  110. } else {
  111. # before or after?
  112. if ($this->isNotTodayAndInTheFuture($date)) {
  113. $when = 1;
  114. } else {
  115. $when = -1;
  116. }
  117. $niceDate = $this->niceDate($date, $format).$timeAttachment; //date("M jS{$y}", $date);
  118. }
  119. if (!empty($whenOverride) && $when == 0) {
  120. if ($this->isInTheFuture($date)) {
  121. $when = 1;
  122. } else {
  123. $when = -1;
  124. }
  125. }
  126. }
  127. if (empty($niceDate) || $when === null) {
  128. $niceDate = '<i>n/a</i>';
  129. } else {
  130. if (!isset($attr['title'])) {
  131. $attr['title'] = $titles[$when];
  132. }
  133. $attr['class'] = 'published '.$whenArray[$when];
  134. //$span = '<span class="published '..'">'; // -1/-2 = ago | 1/2 = ahead | 0 = today
  135. //$spanEnd = '</span>';
  136. }
  137. if (isset($this->Html)) {
  138. return $this->Html->tag('span', $niceDate, $attr);
  139. }
  140. $a = array();
  141. foreach ($attr as $key => $val) {
  142. $a[] = $key.'="'.$val.'"';
  143. }
  144. $attr = '';
  145. if (!empty($a)) {
  146. $attr .= ' '.implode(' ', $a);
  147. }
  148. $span = '<span'.$attr.'>';
  149. $spanEnd = '</span>';
  150. return $span.$niceDate.$spanEnd;
  151. }
  152. /**
  153. * @deprecated - use DatetimeLib::isInRange()
  154. * for birthdays etc
  155. * @param date
  156. * @param string days with +-
  157. * @param options
  158. * 2010-08-26 ms
  159. */
  160. public function isInRangeFromDays($dateString, $days, $options = array()) {
  161. $date = explode(' ',$dateString);
  162. list ($y, $m, $d) = explode('-', $date[0]);
  163. $then = mktime(1, 1, 1, $m, $d, $y);
  164. $now = mktime(1, 1, 1, date('n'), date('j'), $y);
  165. $abs = abs($now-$then);
  166. if ((int)($abs/DAY) <= $days) {
  167. return true;
  168. }
  169. return false;
  170. }
  171. /**
  172. * takes time as hh:mm:ss
  173. * returns hh:mm
  174. * @param badTime
  175. * returns niceTime
  176. * TODO: move to lib, but more generic
  177. * 2011-07-19 gh
  178. */
  179. public function niceTime($badTime) {
  180. return substr($badTime, 0, 5);
  181. }
  182. }