00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00027 define('TYPES_DATE_FMT_DATE', '%1$04s-%2$02s-%3$02s');
00031 define('TYPES_DATE_FMT_DATE_FR', '%3$02s/%2$02s/%1$04s');
00035 define('TYPES_DATA_FMT_HOUR', '%4$02s:%5$02s:%6$02s');
00036
00047 class ODate
00048 {
00049 var
$_year = 0;
00050 var
$_month = 0;
00051 var
$_day = 0;
00052 var
$_hours = 0;
00053 var
$_minutes = 0;
00054 var
$_seconds = 0;
00055
00068 function
__construct($y=0, $mt=0, $d=0,
00069 $h=0, $m=0, $s=0)
00070 {
00071 $this->
setYear($y);
00072 $this->
setMonth($mt);
00073 $this->
setDay($d);
00074 $this->
setHours($h);
00075 $this->
setMinutes($m);
00076 $this->
setSeconds($s);
00077 }
00078
00089 function
ODate($y=0, $mt=0, $d=0,
00090 $h=0, $m=0, $s=0)
00091 {
00092 $this->__construct($y, $mt, $d, $h, $m, $s);
00093 }
00094
00098 function
add($date)
00099 {
00100 $this->
inc($date->getYear(),
00101 $date->getMonth(),
00102 $date->getDay(),
00103 $date->getHours(),
00104 $date->getMinutes(),
00105 $date->getSeconds());
00106 }
00107
00118 function
inc($y,$mt,$d,$h,$m,$s)
00119 {
00120 $this->
addSeconds($s);
00121 $this->
addMinutes($m);
00122 $this->
addHours($h);
00123 $this->
addDays($d);
00124 $this->
addMonths($mt);
00125 $this->
addYears($y);
00126 }
00127
00135 function
setDate($y,$m,$d)
00136 {
00137 $this->
setYear($y);
00138 $this->
setMonth($m);
00139 $this->
setDay($d);
00140 }
00141
00149 function
setTime($y,$m,$d)
00150 {
00151 $this->
setHours($h);
00152 $this->
setMinutes($m);
00153 $this->
setSeconds($s);
00154 }
00155
00161 function
addYears($y)
00162 {
00163 $this->_year += $y;
00164 }
00165
00171 function
addMonths($m)
00172 {
00173 $this->_month += $m;
00174
if ($this->_month > 12) {
00175 $this->addYears($this->_month / 12);
00176 $this->_month = $this->_month % 12;
00177 }
00178 }
00179
00185 function
addDays($d)
00186 {
00187 $m = 0;
00188 $this->_day +=
$d;
00189 $m = $this->
numberOfMonthDays();
00190
while ($this->_day > $m) {
00191 $this->addMonths(1);
00192 $this->_day -= $m;
00193 $m = $this->
numberOfMonthDays();
00194 }
00195 }
00196
00202 function
addHours($h)
00203 {
00204 $this->_hours += $h;
00205
if ($this->_hours >= 24) {
00206 $this->addDays($this->_hours / 24);
00207 $this->_hours = $this->_hours % 24;
00208 }
00209 }
00210
00216 function
addMinutes($m)
00217 {
00218 $this->_minutes += $m;
00219
if ($this->_minutes >= 60) {
00220 $this->addHours($this->_minutes / 60);
00221 $this->_minutes = $this->_minutes % 60;
00222 }
00223 }
00224
00230 function
addSeconds($s)
00231 {
00232 $this->_seconds +=
$s;
00233
if ($this->_seconds >= 60) {
00234 $this->addMinutes($this->_seconds / 60);
00235 $this->_seconds = $this->_seconds % 60;
00236 }
00237 }
00238
00244 function
isBissextile()
00245 {
00246
if ($this->_year % 400 == 0)
return true;
00247
if ($this->_year % 100 == 0)
return false;
00248
if ($this->_year % 4 == 0)
return true;
00249
return false;
00250 }
00251
00257 function
numberOfMonthDays()
00258 {
00259
if ($this->_month == 2 && $this->
isBissextile()) {
00260
return 29;
00261 }
00262 $DOM = array(-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
00263
return $DOM[$this->_month];
00264 }
00265
00269 function
setNow()
00270 {
00271
$d = getdate();
00272 $this->
setSeconds($d['seconds']);
00273 $this->
setMinutes($d['minutes']);
00274 $this->
setHours($d['hours']);
00275 $this->
setDay($d['mday']);
00276 $this->
setMonth($d['mon']);
00277 $this->
setYear($d['year']);
00278 }
00279
00283 function
cleanup()
00284 {
00285 $this->add(0,0,0,0,0,0);
00286 }
00287
00296 function
compare($date)
00297 {
00298
return strcmp($this->toNumericString(),
00299 $date->toNumericString());
00300 }
00301
00307 function
toNumericString()
00308 {
00309
return (
int)
"$this->_year$this->_month$this->_day".
00310
"$this->_hours$this->_minutes$this->_seconds";
00311 }
00312
00321 function
toString($format=
"%04d-%02d-%02d %02d:%02d:%02d")
00322 {
00323 $this->
cleanup();
00324
return sprintf($format,
00325 $this->_year,
00326 $this->_month,
00327 $this->_day,
00328 $this->_hours,
00329 $this->_minutes,
00330 $this->_seconds);
00331 }
00332
00333
00334
00335
00336
00337
00342 function
getYear()
00343 {
00344
return $this->_year;
00345 }
00346
00351 function
getMonth()
00352 {
00353
return $this->_month;
00354 }
00355
00360 function
getDay()
00361 {
00362
return $this->_day;
00363 }
00364
00369 function
getHours()
00370 {
00371
return $this->_hours;
00372 }
00373
00378 function
getMinutes()
00379 {
00380
return $this->_minutes;
00381 }
00382
00387 function
getSeconds()
00388 {
00389
return $this->_seconds;
00390 }
00391
00396 function
setYear($y)
00397 {
00398 $this->_year = $y;
00399 }
00400
00405 function
setMonth($m)
00406 {
00407 $this->_month = $m;
00408 }
00409
00414 function
setDay($d)
00415 {
00416 $this->_day =
$d;
00417 }
00418
00423 function
setHours($h)
00424 {
00425 $this->_hours = $h;
00426 }
00427
00432 function
setMinutes($m)
00433 {
00434 $this->_minutes = $m;
00435 }
00436
00441 function
setSeconds($s)
00442 {
00443 $this->_seconds =
$s;
00444 }
00445 }
00446
00447 ?>