27 $this->timestamp = $this->cfg->timestamp;
36 public function set($date, $type =
'date', $len = null) {
37 $this->isNull =
false;
38 if (is_null($date) || empty($date) || $date ==
'0000-00-00')
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');
60 $formatWork = $format;
61 foreach($elts as $k=>$e) {
62 $tmp = strpos($formatWork, $k);
65 $formatWork = str_replace($k, str_repeat(
'Z', strlen($k)), $formatWork);
68 $formatWork = str_replace(array_keys($elts), $elts, $format);
70 preg_match(
'`'.$formatWork.
'`', $date, $matches);
71 if (!empty($matches)) {
74 foreach($pos as $k=>$v) {
79 $ret[
'y'] = $matches[$i];
83 $key = $k==
'MMM'?
'l':
'm';
85 foreach($this->cfg->month as $vv) {
86 if (strtolower($vv[$key]) == strtolower($matches[$i])) {
93 $ret[
'm'] = $matches[$i];
96 $ret[
'd'] = $matches[$i];
99 $ret[$k] = $matches[$i];
106 }
else if ($type ==
'time') {
107 $time = strtotime($date);
109 $this->
set($tmp[
'y'].
'-'.$tmp[
'm'].
'-'.$tmp[
'd'],
'date');
110 $this->timestamp+= strtotime($date) - strtotime(date(
'Y-m-d'));
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'),
146 $tmp = array_merge($this->
getArray(), $values);
147 $this->timestamp = strtotime($tmp[
'y'].
'-'.$tmp[
'm'].
'-'.$tmp[
'd'].
' '.$tmp[
'h'].
':'.$tmp[
'i'].
':'.$tmp[
's']);
156 public function get($part = null) {
158 return $this->timestamp;
161 return date(
'Y', $this->timestamp);
164 return date($part, $this->timestamp);
176 public function format($type = null, $len = null) {
180 $type = $this->cfg->getInArray(
'defaultFormat',
'type');
182 $len = $this->cfg->getInArray(
'defaultFormat',
'len');
184 $form = $this->cfg->getInArray(
'format'.ucfirst($type), $len);
185 if ($type ==
'datetime') {
187 array(
'date',
'time'),
189 $this->cfg->getInArray(
'formatDate', $len),
190 $this->cfg->getInArray(
'formatTime', $len),
206 $month = $this->cfg->getInArray(
'month',
'm'.intval($time[
'm']));
207 $day = $this->cfg->getInArray(
'day',
'd'.intval($time[
'w']));
209 'H'=>str_pad($time[
'H'], 2,
'0', STR_PAD_LEFT),
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),
216 'YY'=>substr($time[
'y'], -2),
219 'M'=>str_pad($time[
'm'], 2,
'0', STR_PAD_LEFT),
220 'D'=>str_pad($time[
'd'], 2,
'0', STR_PAD_LEFT),
224 $intermediate = array();
226 foreach($places as $k=>$p) {
227 $intermediate[$k] =
'_|_'.$i.
'_|_';
231 $ret = str_replace($intermediate, $places, str_replace(array_keys($intermediate), $intermediate, $format));
232 if ($this->cfg->htmlOut)
243 public function getJs($default =
'""') {
246 return 'new Date('.($this->timestamp*1000).
')';
256 $timestamp = $this->
get() - (is_null($d)? time() : $d->get());
259 $timeago = $this->cfg->getInArray(
'timeago', (
$timestamp < 0) ?
'-' :
'+');
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);
270 foreach($tmp as $k=>$v) {
271 $nb = intval($timestampAbs/$v);
273 return sprintf($timeago[$k][
'one'], 1);
275 return sprintf($timeago[$k][
'mul'], $nb);
278 return $this->cfg->getInArray(
'timeago',
'now');
static htmlOut($val, $key=false)
timeago(helper_date $d=null)
format($type=null, $len=null)