| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- App::uses('DatetimeLib', 'Tools.Lib');
- App::uses('TimeHelper', 'View/Helper');
- class DatetimeHelper extends TimeHelper {
-
- public $helpers = array('Html');
- public $Datetime;
- protected $userOffset = null;
- protected $daylightSavings = false;
- public function __construct($View = null, $settings = array()) {
- parent::__construct($View, $settings);
- $i18n = Configure::read('Localization');
- if (!empty($i18n['time_offset'])) {
- $this->userOffset = (int)$i18n['time_offset'];
- }
- if (!empty($i18n['daylight_savings'])) {
- $this->daylightSavings = (bool)$i18n['daylight_savings'];
- }
- $this->Datetime = new DatetimeLib();
- }
- public function __call($method, $params) {
- if (!method_exists($this, 'call__')) {
- //trigger_error(__('Magic method handler call__ not defined in %s', get_class($this)), E_USER_ERROR);
- }
- return call_user_func_array(array($this->Datetime, $method), $params);
- }
- /**
- * EXPERIMENTAL!!!
- * @param
- * @param
- * @return int offset
- * 2009-03-19 ms
- */
- public function tzOffset($gmtoffset, $is_dst) {
- //global $gmtoffset, $is_dst;
- extract(getdate());
- $serveroffset = gmmktime(0,0,0,$mon,$mday,$year) - mktime(0,0,0,$mon,$mday,$year);
- $offset = $gmtoffset - $serveroffset;
- return $offset + ($is_dst ? 3600 : 0);
- }
- /**
- * @param string date (from db)
- * @return int $age on success, mixed $default otherwise
- * 2009-11-22 ms
- */
- public function userAge($date = null, $default = '---') {
- if ((int)$date === 0) {
- return $default;
- }
- $age = $this->Datetime->age($date, null);
- if ($age >= 1 && $age <= 99) {
- return $age;
- }
- return $default;
- }
- /**
- * Like localDate(), only with additional markup <span> and class="today", if today, etc
- * 2009-11-22 ms
- */
- public function localDateMarkup($dateString = null, $format = null, $options = array()) {
- $date = $this->Datetime->localDate($dateString, $format, $options);
- $date = '<span'.($this->isToday($dateString,(isset($options['userOffset'])?$options['userOffset']:null))?' class="today"':'').'>'.$date.'</span>';
- return $date;
- }
- /**
- * Like niceDate(), only with additional markup <span> and class="today", if today, etc
- * 2009-11-22 ms
- */
- public function niceDateMarkup($dateString = null, $format = null, $options = array()) {
- $date = $this->niceDate($dateString, $format, $options);
- $date = '<span'.($this->isToday($dateString,(isset($options['userOffset'])?$options['userOffset']:null))?' class="today"':'').'>'.$date.'</span>';
- return $date;
- }
- /**
- * returns red/specialGreen/green date depending on the current day
- * @param date in DB Format (xxxx-xx-xx)
- * ...
- * @param array $options
- * @param array $attr: html attributes
- * @return nicely formatted date
- * 2009-07-25 ms
- * // TODO refactor!
- */
- public function published($dateString = null, $userOffset = null, $options=array(), $attr=array()) {
- $date = $dateString ? $this->fromString($dateString, $userOffset) : null; // time() ?
- $niceDate = '';
- $when = null;
- $span = '';
- $spanEnd = '';
- $whenArray = array('-1'=>'already','0'=>'today','1'=>'notyet');
- $titles = array('-1'=>__('publishedAlready'),'0'=>__('publishedToday'),'1'=>__('publishedNotYet'));
- if (!empty($date)) {
- $y = $this->isThisYear($date) ? '' : ' Y';
- $format = (!empty($options['format'])?$options['format']:FORMAT_NICE_YMD);
- # Hack
- # //TODO: get this to work with datetime - somehow cleaner
- $timeAttachment = '';
- if (isset($options['niceDateTime'])) {
- $timeAttachment = ', '.$this->niceDate($date, $options['niceDateTime']);
- $whenOverride = true;
- }
- if ($this->isToday($date)) {
- $when = 0;
- $niceDate = __('Today').$timeAttachment;
- } elseif ($this->isTomorrow($date)) {
- $when = 1;
- $niceDate = __('Tomorrow').$timeAttachment;
- } elseif ($this->wasYesterday($date)) {
- $when = -1;
- $niceDate = __('Yesterday').$timeAttachment;
- } else {
- # before or after?
- if ($this->isNotTodayAndInTheFuture($date)) {
- $when = 1;
- } else {
- $when = -1;
- }
- $niceDate = $this->niceDate($date, $format).$timeAttachment; //date("M jS{$y}", $date);
- }
- if (!empty($whenOverride) && $when == 0) {
- if ($this->isInTheFuture($date)) {
- $when = 1;
- } else {
- $when = -1;
- }
- }
- }
- if (empty($niceDate) || $when === null) {
- $niceDate = '<i>n/a</i>';
- } else {
- if (!isset($attr['title'])) {
- $attr['title'] = $titles[$when];
- }
- $attr['class'] = 'published '.$whenArray[$when];
- //$span = '<span class="published '..'">'; // -1/-2 = ago | 1/2 = ahead | 0 = today
- //$spanEnd = '</span>';
- }
- if (isset($this->Html)) {
- return $this->Html->tag('span', $niceDate, $attr);
- }
- $a = array();
- foreach ($attr as $key => $val) {
- $a[] = $key.'="'.$val.'"';
- }
- $attr = '';
- if (!empty($a)) {
- $attr .= ' '.implode(' ', $a);
- }
- $span = '<span'.$attr.'>';
- $spanEnd = '</span>';
- return $span.$niceDate.$spanEnd;
- }
- /**
- * @deprecated - use DatetimeLib::isInRange()
- * for birthdays etc
- * @param date
- * @param string days with +-
- * @param options
- * 2010-08-26 ms
- */
- public function isInRangeFromDays($dateString, $days, $options = array()) {
- $date = explode(' ',$dateString);
- list ($y, $m, $d) = explode('-', $date[0]);
- $then = mktime(1, 1, 1, $m, $d, $y);
- $now = mktime(1, 1, 1, date('n'), date('j'), $y);
- $abs = abs($now-$then);
- if ((int)($abs/DAY) <= $days) {
- return true;
- }
- return false;
- }
- /**
- * takes time as hh:mm:ss
- * returns hh:mm
- * @param badTime
- * returns niceTime
- * TODO: move to lib, but more generic
- * 2011-07-19 gh
- */
- public function niceTime($badTime) {
- return substr($badTime, 0, 5);
- }
- }
|