nyroFwk  0.2
helper/date.class.php
Go to the documentation of this file.
1 <?php
10 class helper_date extends object {
11 
17  protected $timestamp;
18 
24  protected $isNull = false;
25 
26  protected function afterinit() {
27  $this->timestamp = $this->cfg->timestamp;
28  }
29 
36  public function set($date, $type = 'date', $len = null) {
37  $this->isNull = false;
38  if (is_null($date) || empty($date) || $date == '0000-00-00')
39  $this->isNull = true;
40  else if ($type == 'timestamp')
41  $this->timestamp = $date;
42  else if ($type == 'date')
43  $this->timestamp = strtotime($date);
44  else if ($type == 'formatDate') {
45  $format = $this->cfg->getInArray('formatDate', $len);
46  $elts = array('h', 'i', 's', 'o', 'YYYY', 'YY', 'MMM', 'MM', 'M', 'D', 'EEE', 'EE');
47  $elts = array(
48  'h'=>'([0-9]{1,2})',
49  'i'=>'([0-9]{1,2})',
50  's'=>'([0-9]{1,2})',
51  'o'=>'([0-9]{4})',
52  'YYYY'=>'([0-9]{4})',
53  'YY'=>'([0-9]{2})',
54  'MMM'=>'(.*)',
55  'MM'=>'(.{3})',
56  'M'=>'([0-9]{1,2})',
57  'D'=>'([0-9]{1,2})'
58  );
59  $pos = array();
60  $formatWork = $format;
61  foreach($elts as $k=>$e) {
62  $tmp = strpos($formatWork, $k);
63  if ($tmp !== false) {
64  $pos[$k] = $tmp;
65  $formatWork = str_replace($k, str_repeat('Z', strlen($k)), $formatWork);
66  }
67  }
68  $formatWork = str_replace(array_keys($elts), $elts, $format);
69  asort($pos);
70  preg_match('`'.$formatWork.'`', $date, $matches);
71  if (!empty($matches)) {
72  $ret = array();
73  $i=1;
74  foreach($pos as $k=>$v) {
75  $key = null;
76  switch($k) {
77  case 'YYYY':
78  case 'YY':
79  $ret['y'] = $matches[$i];
80  break;
81  case 'MMM':
82  case 'MM':
83  $key = $k=='MMM'?'l':'m';
84  $j = 1;
85  foreach($this->cfg->month as $vv) {
86  if (strtolower($vv[$key]) == strtolower($matches[$i])) {
87  $ret['m'] = $j;
88  }
89  $j++;
90  }
91  break;
92  case 'M':
93  $ret['m'] = $matches[$i];
94  break;
95  case 'D':
96  $ret['d'] = $matches[$i];
97  break;
98  default:
99  $ret[$k] = $matches[$i];
100  break;
101  }
102  $i++;
103  }
104  $this->setArray($ret);
105  }
106  } else if ($type == 'time') {
107  $time = strtotime($date);
108  $tmp = $this->getArray();
109  $this->set($tmp['y'].'-'.$tmp['m'].'-'.$tmp['d'], 'date');
110  $this->timestamp+= strtotime($date) - strtotime(date('Y-m-d'));
111  } else {
112  $tmp = $this->getArray();
113  $tmp[$type] = $date;
114  $this->setArray($tmp);
115  }
116  }
117 
123  public function getArray() {
124  return array(
125  'y'=>$this->get('y'),
126  'm'=>$this->get('m'),
127  'd'=>$this->get('d'),
128  'l'=>$this->get('l'),
129  'w'=>$this->get('w'),
130  'h'=>$this->get('h'),
131  'A'=>$this->get('A'),
132  'H'=>$this->get('H'),
133  'i'=>$this->get('i'),
134  's'=>$this->get('s'),
135  'o'=>$this->get('o')
136  );
137  }
138 
145  public function setArray(array $values) {
146  $tmp = array_merge($this->getArray(), $values);
147  $this->timestamp = strtotime($tmp['y'].'-'.$tmp['m'].'-'.$tmp['d'].' '.$tmp['h'].':'.$tmp['i'].':'.$tmp['s']);
148  }
149 
156  public function get($part = null) {
157  if (is_null($part))
158  return $this->timestamp;
159  switch($part) {
160  case 'y':
161  return date('Y', $this->timestamp);
162  break;
163  default:
164  return date($part, $this->timestamp);
165  break;
166  }
167  }
168 
176  public function format($type = null, $len = null) {
177  if ($this->isNull)
178  return null;
179  if (is_null($type))
180  $type = $this->cfg->getInArray('defaultFormat', 'type');
181  if (is_null($len))
182  $len = $this->cfg->getInArray('defaultFormat', 'len');
183 
184  $form = $this->cfg->getInArray('format'.ucfirst($type), $len);
185  if ($type == 'datetime') {
186  $form = str_replace(
187  array('date', 'time'),
188  array(
189  $this->cfg->getInArray('formatDate', $len),
190  $this->cfg->getInArray('formatTime', $len),
191  ),
192  $form);
193  }
194 
195  return $this->formatDirect($form);
196  }
197 
204  public function formatDirect($format) {
205  $time = $this->getArray();
206  $month = $this->cfg->getInArray('month', 'm'.intval($time['m']));
207  $day = $this->cfg->getInArray('day', 'd'.intval($time['w']));
208  $places = array(
209  'H'=>str_pad($time['H'], 2, '0', STR_PAD_LEFT),
210  'A'=>$time['A'],
211  'h'=>str_pad($time['h'], 2, '0', STR_PAD_LEFT),
212  'i'=>str_pad($time['i'], 2, '0', STR_PAD_LEFT),
213  's'=>str_pad($time['s'], 2, '0', STR_PAD_LEFT),
214  'o'=>($time['o'] <0? '-' : '+').str_pad($time['o'], 4, '0', STR_PAD_LEFT),
215  'YYYY'=>$time['y'],
216  'YY'=>substr($time['y'], -2),
217  'MMM'=>$month['l'],
218  'MM'=>$month['m'],
219  'M'=>str_pad($time['m'], 2, '0', STR_PAD_LEFT),
220  'D'=>str_pad($time['d'], 2, '0', STR_PAD_LEFT),
221  'EEE'=>$day['l'],
222  'EE'=>$day['m'],
223  );
224  $intermediate = array();
225  $i = 0;
226  foreach($places as $k=>$p) {
227  $intermediate[$k] = '_|_'.$i.'_|_';
228  $i++;
229  }
230 
231  $ret = str_replace($intermediate, $places, str_replace(array_keys($intermediate), $intermediate, $format));
232  if ($this->cfg->htmlOut)
233  $ret = utils::htmlOut($ret);
234  return $ret;
235  }
236 
243  public function getJs($default = '""') {
244  if ($this->isNull)
245  return $default;
246  return 'new Date('.($this->timestamp*1000).')';
247  }
248 
255  public function timeago(helper_date $d = null) {
256  $timestamp = $this->get() - (is_null($d)? time() : $d->get());
257  $timestampAbs = abs($timestamp);
258 
259  $timeago = $this->cfg->getInArray('timeago', ($timestamp < 0) ? '-' : '+');
260  $tmp = array();
261  $tmp['s'] = 1;
262  $tmp['i'] = 60;
263  $tmp['h'] = $tmp['i']*60;
264  $tmp['d'] = $tmp['h']*24;
265  $tmp['w'] = $tmp['d']*7;
266  $tmp['m'] = $tmp['d']*30;
267  $tmp['y'] = $tmp['d']*365;
268  $tmp = array_reverse($tmp, true);
269 
270  foreach($tmp as $k=>$v) {
271  $nb = intval($timestampAbs/$v);
272  if ($nb == 1)
273  return sprintf($timeago[$k]['one'], 1);
274  else if ($nb > 1)
275  return sprintf($timeago[$k]['mul'], $nb);
276  }
277 
278  return $this->cfg->getInArray('timeago', 'now');
279  }
280 
281  public function __toString() {
282  return $this->format();
283  }
284 
285 }
static htmlOut($val, $key=false)
formatDirect($format)
timeago(helper_date $d=null)
setArray(array $values)
format($type=null, $len=null)
getJs($default='""')
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13