2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
4 // Copyright (c) 2003 Laurent Bedubourg
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr>
25 * Generic date part format.
27 define('TYPES_DATE_FMT_DATE', '%1$04s-%2$02s-%3$02s');
29 * French date part format.
31 define('TYPES_DATE_FMT_DATE_FR', '%3$02s/%2$02s/%1$04s');
35 define('TYPES_DATA_FMT_HOUR', '%4$02s:%5$02s:%6$02s');
38 * Simple date manipulation object.
43 * ODate::toString() can take a sprintf() format string as first parameter.
45 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
59 * This constructor is for php v4.4 compliance.
61 * @param int $y optional -- year
62 * @param int $mt optional -- month
63 * @param int $d optional -- day
64 * @param int $h optional -- hour
65 * @param int $m optional -- minutes
66 * @param int $s optional -- seconds
68 function __construct($y=0, $mt=0, $d=0,
75 $this->setMinutes($m);
76 $this->setSeconds($s);
82 * @param int $y optional -- year
83 * @param int $mt optional -- month
84 * @param int $d optional -- day
85 * @param int $h optional -- hour
86 * @param int $m optional -- minutes
87 * @param int $s optional -- seconds
89 function ODate($y=0, $mt=0, $d=0,
92 $this->__construct($y, $mt, $d, $h, $m, $s);
96 * Add a date to current date.
100 $this->inc($date->getYear(),
105 $date->getSeconds());
109 * Add values to this date.
111 * @param int $y optional -- year
112 * @param int $mt optional -- month
113 * @param int $d optional -- day
114 * @param int $h optional -- hour
115 * @param int $m optional -- minutes
116 * @param int $s optional -- seconds
118 function inc($y,$mt,$d,$h,$m,$s)
120 $this->addSeconds($s);
121 $this->addMinutes($m);
124 $this->addMonths($mt);
131 * @param int $y -- year
132 * @param int $mt -- month
133 * @param int $d -- day
135 function setDate($y,$m,$d)
145 * @param int $h -- hour
146 * @param int $m -- minutes
147 * @param int $s -- seconds
149 function setTime($y,$m,$d)
152 $this->setMinutes($m);
153 $this->setSeconds($s);
157 * Add some years to this date.
159 * @param int $y -- year
161 function addYears($y)
167 * Add some months to this date.
169 * @param int $m -- month
171 function addMonths($m)
174 if ($this->_month
> 12) {
175 $this->addYears($this->_month
/ 12);
176 $this->_month
= $this->_month %
12;
181 * Add some days to this date.
183 * @param int $d -- days
189 $m = $this->numberOfMonthDays();
190 while ($this->_day
> $m) {
193 $m = $this->numberOfMonthDays();
198 * Add some hours to this date.
200 * @param int $h -- hours
202 function addHours($h)
205 if ($this->_hours
>= 24) {
206 $this->addDays($this->_hours
/ 24);
207 $this->_hours
= $this->_hours %
24;
212 * Add minutes to this date.
214 * @param int $m -- minutes
216 function addMinutes($m)
218 $this->_minutes +
= $m;
219 if ($this->_minutes
>= 60) {
220 $this->addHours($this->_minutes
/ 60);
221 $this->_minutes
= $this->_minutes %
60;
226 * Add seconds to this date.
228 * @param int $s -- seconds
230 function addSeconds($s)
232 $this->_seconds +
= $s;
233 if ($this->_seconds
>= 60) {
234 $this->addMinutes($this->_seconds
/ 60);
235 $this->_seconds
= $this->_seconds %
60;
240 * Return true if bissextile year.
244 function isBissextile()
246 if ($this->_year %
400 == 0) return true;
247 if ($this->_year %
100 == 0) return false;
248 if ($this->_year %
4 == 0) return true;
253 * Get the number of days in current month
257 function numberOfMonthDays()
259 if ($this->_month
== 2 && $this->isBissextile()) {
262 $DOM = array(-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
263 return $DOM[$this->_month
];
267 * Set now time values to this object.
272 $this->setSeconds($d['seconds']);
273 $this->setMinutes($d['minutes']);
274 $this->setHours($d['hours']);
275 $this->setDay($d['mday']);
276 $this->setMonth($d['mon']);
277 $this->setYear($d['year']);
281 * Ensure that date is correct.
285 $this->add(0,0,0,0,0,0);
289 * Compare with another date, return < 0 if less, == 0 if equals, > 0 if
292 * @param ODate $date -- date to compare with
294 * @return int (strcmp)
296 function compare($date)
298 return strcmp($this->toNumericString(),
299 $date->toNumericString());
303 * Return a concatenation of date fields (numeric date).
307 function toNumericString()
309 return (int)"$this->_year$this->_month$this->_day".
310 "$this->_hours$this->_minutes$this->_seconds";
314 * Simple date format.
316 * @param string $format optional
317 * Format to pass to sprintf (ex:"%04d-%02d-%02d %02d:%02d:%02d")
321 function toString($format="%04d-%02d-%02d %02d:%02d:%02d")
324 return sprintf($format,
335 // OTHER GETTER SETTERS
353 return $this->_month
;
371 return $this->_hours
;
378 function getMinutes()
380 return $this->_minutes
;
387 function getSeconds()
389 return $this->_seconds
;
394 * @param int $y -- year
403 * @param int $m -- month
405 function setMonth($m)
412 * @param int $d -- day
421 * @param int $h -- hours
423 function setHours($h)
430 * @param int $m -- minutes
432 function setMinutes($m)
434 $this->_minutes
= $m;
439 * @param int $s -- seconds
441 function setSeconds($s)
443 $this->_seconds
= $s;