DatetimeHelper.php 5.8 KB

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