DatetimeHelper.php 5.4 KB

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