00001 <?php
00010 class helper_date extends object {
00011
00017 protected $timestamp;
00018
00024 protected $isNull = false;
00025
00026 protected function afterinit() {
00027 $this->timestamp = $this->cfg->timestamp;
00028 }
00029
00036 public function set($date, $type='date', $len=null) {
00037 $this->isNull = false;
00038 if (is_null($date) || empty($date) || $date == '0000-00-00')
00039 $this->isNull = true;
00040 else if ($type == 'timestamp')
00041 $this->timestamp = $date;
00042 else if ($type == 'date')
00043 $this->timestamp = strtotime($date);
00044 else if ($type == 'formatDate') {
00045 $format = $this->cfg->getInArray('formatDate', $len);
00046 $elts = array('h', 'i', 's', 'o', 'YYYY', 'YY', 'MMM', 'MM', 'M', 'D', 'EEE', 'EE');
00047 $elts = array(
00048 'h'=>'([0-9]{1,2})',
00049 'i'=>'([0-9]{1,2})',
00050 's'=>'([0-9]{1,2})',
00051 'o'=>'([0-9]{4})',
00052 'YYYY'=>'([0-9]{4})',
00053 'YY'=>'([0-9]{2})',
00054 'MMM'=>'(.*)',
00055 'MM'=>'(.{3})',
00056 'M'=>'([0-9]{1,2})',
00057 'D'=>'([0-9]{1,2})'
00058 );
00059 $pos = array();
00060 $formatWork = $format;
00061 foreach($elts as $k=>$e) {
00062 $tmp = strpos($formatWork, $k);
00063 if ($tmp !== false) {
00064 $pos[$k] = $tmp;
00065 $formatWork = str_replace($k, str_repeat('Z', strlen($k)), $formatWork);
00066 }
00067 }
00068 $formatWork = str_replace(array_keys($elts), $elts, $format);
00069 asort($pos);
00070 preg_match('`'.$formatWork.'`', $date, $matches);
00071 if (!empty($matches)) {
00072 $ret = array();
00073 $i=1;
00074 foreach($pos as $k=>$v) {
00075 $key = null;
00076 switch($k) {
00077 case 'YYYY':
00078 case 'YY':
00079 $ret['y'] = $matches[$i];
00080 break;
00081 case 'MMM':
00082 case 'MM':
00083 $key = $k=='MMM'?'l':'m';
00084 $j = 1;
00085 foreach($this->cfg->month as $vv) {
00086 if (strtolower($vv[$key]) == strtolower($matches[$i])) {
00087 $ret['m'] = $j;
00088 }
00089 $j++;
00090 }
00091 break;
00092 case 'M':
00093 $ret['m'] = $matches[$i];
00094 break;
00095 case 'D':
00096 $ret['d'] = $matches[$i];
00097 break;
00098 default:
00099 $ret[$k] = $matches[$i];
00100 break;
00101 }
00102 $i++;
00103 }
00104 $this->setArray($ret);
00105 }
00106 } else if ($type == 'time') {
00107 $time = strtotime($date);
00108 $tmp = $this->getArray();
00109 $this->set($tmp['y'].'-'.$tmp['m'].'-'.$tmp['d'], 'date');
00110 $this->timestamp+= strtotime($date) - strtotime(date('Y-m-d'));
00111 } else {
00112 $tmp = $this->getArray();
00113 $tmp[$type] = $date;
00114 $this->setArray($tmp);
00115 }
00116 }
00117
00123 public function getArray() {
00124 return array(
00125 'y'=>$this->get('y'),
00126 'm'=>$this->get('m'),
00127 'd'=>$this->get('d'),
00128 'l'=>$this->get('l'),
00129 'w'=>$this->get('w'),
00130 'h'=>$this->get('h'),
00131 'A'=>$this->get('A'),
00132 'H'=>$this->get('H'),
00133 'i'=>$this->get('i'),
00134 's'=>$this->get('s'),
00135 'o'=>$this->get('o')
00136 );
00137 }
00138
00145 public function setArray(array $values) {
00146 $tmp = array_merge($this->getArray(), $values);
00147 $this->timestamp = strtotime($tmp['y'].'-'.$tmp['m'].'-'.$tmp['d'].' '.$tmp['h'].':'.$tmp['i'].':'.$tmp['s']);
00148 }
00149
00156 public function get($part=null) {
00157 if (is_null($part))
00158 return $this->timestamp;
00159 switch($part) {
00160 case 'y':
00161 return date('Y', $this->timestamp);
00162 break;
00163 default:
00164 return date($part, $this->timestamp);
00165 break;
00166 }
00167 }
00168
00176 public function format($type=null, $len=null) {
00177 if ($this->isNull)
00178 return null;
00179 if (is_null($type))
00180 $type = $this->cfg->getInArray('defaultFormat', 'type');
00181 if (is_null($len))
00182 $len = $this->cfg->getInArray('defaultFormat', 'len');
00183
00184 $time = $this->getArray();
00185 $form = $this->cfg->getInArray('format'.ucfirst($type), $len);
00186 if ($type == 'datetime') {
00187 $form = str_replace(
00188 array('date', 'time'),
00189 array(
00190 $this->cfg->getInArray('formatDate', $len),
00191 $this->cfg->getInArray('formatTime', $len),
00192 ),
00193 $form);
00194 }
00195
00196 $month = $this->cfg->getInArray('month', 'm'.intval($time['m']));
00197 $day = $this->cfg->getInArray('day', 'd'.intval($time['w']));
00198 $places = array(
00199 'H'=>str_pad($time['H'], 2, '0', STR_PAD_LEFT),
00200 'A'=>$time['A'],
00201 'h'=>str_pad($time['h'], 2, '0', STR_PAD_LEFT),
00202 'i'=>str_pad($time['i'], 2, '0', STR_PAD_LEFT),
00203 's'=>str_pad($time['s'], 2, '0', STR_PAD_LEFT),
00204 'o'=>($time['o'] <0? '-' : '+').str_pad($time['o'], 4, '0', STR_PAD_LEFT),
00205 'YYYY'=>$time['y'],
00206 'YY'=>substr($time['y'], -2),
00207 'MMM'=>$month['l'],
00208 'MM'=>$month['m'],
00209 'M'=>str_pad($time['m'], 2, '0', STR_PAD_LEFT),
00210 'D'=>str_pad($time['d'], 2, '0', STR_PAD_LEFT),
00211 'EEE'=>$day['l'],
00212 'EE'=>$day['m'],
00213 );
00214 $intermediate = array();
00215 $i = 0;
00216 foreach($places as $k=>$p) {
00217 $intermediate[$k] = '_|_'.$i.'_|_';
00218 $i++;
00219 }
00220
00221 $ret = str_replace($intermediate, $places, str_replace(array_keys($intermediate), $intermediate, $form));
00222 if ($this->cfg->htmlOut)
00223 $ret = utils::htmlOut($ret);
00224 return $ret;
00225 }
00226
00233 public function getJs($default='""') {
00234 if ($this->isNull)
00235 return $default;
00236 return 'new Date('.($this->timestamp*1000).')';
00237 }
00238
00245 public function timeago(helper_date $d=null) {
00246 $timestamp = $this->get() - (is_null($d)? time() : $d->get());
00247 $timestampAbs = abs($timestamp);
00248
00249 $timeago = $this->cfg->getInArray('timeago', ($timestamp < 0) ? '-' : '+');
00250 $tmp = array();
00251 $tmp['s'] = 1;
00252 $tmp['i'] = 60;
00253 $tmp['h'] = $tmp['i']*60;
00254 $tmp['d'] = $tmp['h']*24;
00255 $tmp['w'] = $tmp['d']*7;
00256 $tmp['m'] = $tmp['d']*30;
00257 $tmp['y'] = $tmp['d']*365;
00258 $tmp = array_reverse($tmp, true);
00259
00260 foreach($tmp as $k=>$v) {
00261 $nb = intval($timestampAbs/$v);
00262 if ($nb == 1)
00263 return sprintf($timeago[$k]['one'], 1);
00264 else if ($nb > 1)
00265 return sprintf($timeago[$k]['mul'], $nb);
00266 }
00267
00268 return $this->cfg->getInArray('timeago', 'now');
00269 }
00270
00271 public function __toString() {
00272 return $this->format();
00273 }
00274
00275 }