2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
\r
7 * Generic date handling class for PEAR
\r
9 * Generic date handling class for PEAR. Attempts to be time zone aware
\r
10 * through the Date::TimeZone class. Supports several operations from
\r
11 * Date::Calc on Date objects.
\r
13 * PHP versions 4 and 5
\r
17 * Copyright (c) 1997-2006 Baba Buehler, Pierre-Alain Joye
\r
18 * All rights reserved.
\r
20 * Redistribution and use in source and binary forms, with or without
\r
21 * modification, are permitted under the terms of the BSD License.
\r
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
\r
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
\r
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
\r
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
\r
27 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
\r
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
\r
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
\r
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
\r
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
\r
33 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
34 * POSSIBILITY OF SUCH DAMAGE.
\r
36 * @category Date and Time
\r
38 * @author Baba Buehler <baba@babaz.com>
\r
39 * @author Pierre-Alain Joye <pajoye@php.net>
\r
40 * @author Firman Wandayandi <firman@php.net>
\r
41 * @copyright 1997-2006 Baba Buehler, Pierre-Alain Joye
\r
42 * @license http://www.opensource.org/licenses/bsd-license.php
\r
44 * @version CVS: $Id: Date.php,v 1.41 2006/11/22 00:28:03 firman Exp $
\r
45 * @link http://pear.php.net/package/Date
\r
53 * Load Date_TimeZone.
\r
55 require_once 'Date/TimeZone.php';
\r
60 require_once 'Date/Calc.php';
\r
65 require_once 'Date/Span.php';
\r
70 // {{{ Output formats Pass this to getDate().
\r
73 * "YYYY-MM-DD HH:MM:SS"
\r
75 define('DATE_FORMAT_ISO', 1);
\r
78 * "YYYYMMSSTHHMMSS(Z|(+/-)HHMM)?"
\r
80 define('DATE_FORMAT_ISO_BASIC', 2);
\r
83 * "YYYY-MM-SSTHH:MM:SS(Z|(+/-)HH:MM)?"
\r
85 define('DATE_FORMAT_ISO_EXTENDED', 3);
\r
88 * "YYYY-MM-SSTHH:MM:SS(.S*)?(Z|(+/-)HH:MM)?"
\r
90 define('DATE_FORMAT_ISO_EXTENDED_MICROTIME', 6);
\r
95 define('DATE_FORMAT_TIMESTAMP', 4);
\r
98 * long int, seconds since the unix epoch
\r
100 define('DATE_FORMAT_UNIXTIME', 5);
\r
108 * Generic date handling class for PEAR
\r
110 * Generic date handling class for PEAR. Attempts to be time zone aware
\r
111 * through the Date::TimeZone class. Supports several operations from
\r
112 * Date::Calc on Date objects.
\r
114 * @author Baba Buehler <baba@babaz.com>
\r
115 * @author Pierre-Alain Joye <pajoye@php.net>
\r
116 * @author Firman Wandayandi <firman@php.net>
\r
117 * @copyright 1997-2006 Baba Buehler, Pierre-Alain Joye
\r
118 * @license http://www.opensource.org/licenses/bsd-license.php
\r
120 * @version Release: 1.4.7
\r
121 * @link http://pear.php.net/package/Date
\r
164 * the parts of a second
\r
170 * timezone for this date
\r
171 * @var object Date_TimeZone
\r
176 * define the default weekday abbreviation length
\r
177 * used by ::format()
\r
180 var $getWeekdayAbbrnameLength = 3;
\r
188 * Creates a new Date Object initialized to the current date/time in the
\r
189 * system-default timezone by default. A date optionally
\r
190 * passed in may be in the ISO 8601, TIMESTAMP or UNIXTIME format,
\r
191 * or another Date object. If no date is passed, the current date/time
\r
196 * @param mixed $date optional - date/time to initialize
\r
197 * @return object Date the new Date object
\r
199 function __construct($date = null)
\r
201 $this->tz = Date_TimeZone::getDefault();
\r
202 if (is_null($date)) {
\r
203 $this->setDate(date("Y-m-d H:i:s"));
\r
204 } elseif (is_a($date, 'Date')) {
\r
205 $this->copy($date);
\r
207 $this->setDate($date);
\r
215 * Set the fields of a Date object based on the input date and format
\r
217 * Set the fields of a Date object based on the input date and format,
\r
218 * which is specified by the DATE_FORMAT_* constants.
\r
221 * @param string $date input date
\r
222 * @param int $format Optional format constant (DATE_FORMAT_*) of the input date.
\r
223 * This parameter isn't really needed anymore, but you could
\r
224 * use it to force DATE_FORMAT_UNIXTIME.
\r
226 function setDate($date, $format = DATE_FORMAT_ISO)
\r
229 preg_match('/^(\d{4})-?(\d{2})-?(\d{2})([T\s]?(\d{2}):?(\d{2}):?(\d{2})(\.\d+)?(Z|[\+\-]\d{2}:?\d{2})?)?$/i', $date, $regs)
\r
230 && $format != DATE_FORMAT_UNIXTIME) {
\r
231 // DATE_FORMAT_ISO, ISO_BASIC, ISO_EXTENDED, and TIMESTAMP
\r
232 // These formats are extremely close to each other. This regex
\r
233 // is very loose and accepts almost any butchered format you could
\r
234 // throw at it. e.g. 2003-10-07 19:45:15 and 2003-10071945:15
\r
235 // are the same thing in the eyes of this regex, even though the
\r
236 // latter is not a valid ISO 8601 date.
\r
237 $this->year = $regs[1];
\r
238 $this->month = $regs[2];
\r
239 $this->day = $regs[3];
\r
240 $this->hour = isset($regs[5])?$regs[5]:0;
\r
241 $this->minute = isset($regs[6])?$regs[6]:0;
\r
242 $this->second = isset($regs[7])?$regs[7]:0;
\r
243 $this->partsecond = isset($regs[8])?(float)$regs[8]:(float)0;
\r
245 // if an offset is defined, convert time to UTC
\r
246 // Date currently can't set a timezone only by offset,
\r
247 // so it has to store it as UTC
\r
248 if (isset($regs[9])) {
\r
249 $this->toUTCbyOffset($regs[9]);
\r
251 } elseif (is_numeric($date)) {
\r
253 $this->setDate(date("Y-m-d H:i:s", $date));
\r
262 $this->partsecond = (float)0;
\r
270 * Get a string (or other) representation of this date
\r
272 * Get a string (or other) representation of this date in the
\r
273 * format specified by the DATE_FORMAT_* constants.
\r
276 * @param int $format format constant (DATE_FORMAT_*) of the output date
\r
277 * @return string the date in the requested format
\r
279 function getDate($format = DATE_FORMAT_ISO)
\r
282 case DATE_FORMAT_ISO:
\r
283 return $this->format("%Y-%m-%d %T");
\r
285 case DATE_FORMAT_ISO_BASIC:
\r
286 $format = "%Y%m%dT%H%M%S";
\r
287 if ($this->tz->getID() == 'UTC') {
\r
290 return $this->format($format);
\r
292 case DATE_FORMAT_ISO_EXTENDED:
\r
293 $format = "%Y-%m-%dT%H:%M:%S";
\r
294 if ($this->tz->getID() == 'UTC') {
\r
297 return $this->format($format);
\r
299 case DATE_FORMAT_ISO_EXTENDED_MICROTIME:
\r
300 $format = "%Y-%m-%dT%H:%M:%s";
\r
301 if ($this->tz->getID() == 'UTC') {
\r
304 return $this->format($format);
\r
306 case DATE_FORMAT_TIMESTAMP:
\r
307 return $this->format("%Y%m%d%H%M%S");
\r
309 case DATE_FORMAT_UNIXTIME:
\r
310 return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
\r
319 * Copy values from another Date object
\r
321 * Makes this Date a copy of another Date object.
\r
324 * @param object Date $date Date to copy from
\r
326 function copy($date)
\r
328 $this->year = $date->year;
\r
329 $this->month = $date->month;
\r
330 $this->day = $date->day;
\r
331 $this->hour = $date->hour;
\r
332 $this->minute = $date->minute;
\r
333 $this->second = $date->second;
\r
334 $this->tz = $date->tz;
\r
341 * Date pretty printing, similar to strftime()
\r
343 * Formats the date in the given format, much like
\r
344 * strftime(). Most strftime() options are supported.<br><br>
\r
346 * formatting options:<br><br>
\r
348 * <code>%a </code> abbreviated weekday name (Sun, Mon, Tue) <br>
\r
349 * <code>%A </code> full weekday name (Sunday, Monday, Tuesday) <br>
\r
350 * <code>%b </code> abbreviated month name (Jan, Feb, Mar) <br>
\r
351 * <code>%B </code> full month name (January, February, March) <br>
\r
352 * <code>%C </code> century number (the year divided by 100 and truncated to an integer, range 00 to 99) <br>
\r
353 * <code>%d </code> day of month (range 00 to 31) <br>
\r
354 * <code>%D </code> same as "%m/%d/%y" <br>
\r
355 * <code>%e </code> day of month, single digit (range 0 to 31) <br>
\r
356 * <code>%E </code> number of days since unspecified epoch (integer, Date_Calc::dateToDays()) <br>
\r
357 * <code>%H </code> hour as decimal number (00 to 23) <br>
\r
358 * <code>%I </code> hour as decimal number on 12-hour clock (01 to 12) <br>
\r
359 * <code>%j </code> day of year (range 001 to 366) <br>
\r
360 * <code>%m </code> month as decimal number (range 01 to 12) <br>
\r
361 * <code>%M </code> minute as a decimal number (00 to 59) <br>
\r
362 * <code>%n </code> newline character (\n) <br>
\r
363 * <code>%O </code> dst-corrected timezone offset expressed as "+/-HH:MM" <br>
\r
364 * <code>%o </code> raw timezone offset expressed as "+/-HH:MM" <br>
\r
365 * <code>%p </code> either 'am' or 'pm' depending on the time <br>
\r
366 * <code>%P </code> either 'AM' or 'PM' depending on the time <br>
\r
367 * <code>%r </code> time in am/pm notation, same as "%I:%M:%S %p" <br>
\r
368 * <code>%R </code> time in 24-hour notation, same as "%H:%M" <br>
\r
369 * <code>%s </code> seconds including the decimal representation smaller than one second <br>
\r
370 * <code>%S </code> seconds as a decimal number (00 to 59) <br>
\r
371 * <code>%t </code> tab character (\t) <br>
\r
372 * <code>%T </code> current time, same as "%H:%M:%S" <br>
\r
373 * <code>%w </code> weekday as decimal (0 = Sunday) <br>
\r
374 * <code>%U </code> week number of current year, first sunday as first week <br>
\r
375 * <code>%y </code> year as decimal (range 00 to 99) <br>
\r
376 * <code>%Y </code> year as decimal including century (range 0000 to 9999) <br>
\r
377 * <code>%% </code> literal '%' <br>
\r
381 * @param string format the format string for returned date/time
\r
382 * @return string date/time in given format
\r
384 function format($format)
\r
388 for($strpos = 0; $strpos < strlen($format); $strpos++) {
\r
389 $char = substr($format,$strpos,1);
\r
390 if ($char == "%") {
\r
391 $nextchar = substr($format,$strpos + 1,1);
\r
392 switch ($nextchar) {
\r
394 $output .= Date_Calc::getWeekdayAbbrname($this->day,$this->month,$this->year, $this->getWeekdayAbbrnameLength);
\r
397 $output .= Date_Calc::getWeekdayFullname($this->day,$this->month,$this->year);
\r
400 $output .= Date_Calc::getMonthAbbrname($this->month);
\r
403 $output .= Date_Calc::getMonthFullname($this->month);
\r
406 $output .= sprintf("%02d",intval($this->year/100));
\r
409 $output .= sprintf("%02d",$this->day);
\r
412 $output .= sprintf("%02d/%02d/%02d",$this->month,$this->day,$this->year);
\r
415 $output .= $this->day * 1; // get rid of leading zero
\r
418 $output .= Date_Calc::dateToDays($this->day,$this->month,$this->year);
\r
421 $output .= sprintf("%02d", $this->hour);
\r
424 $output .= sprintf("%d", $this->hour);
\r
427 $hour = ($this->hour + 1) > 12 ? $this->hour - 12 : $this->hour;
\r
428 $output .= sprintf("%02d", $hour==0 ? 12 : $hour);
\r
431 $hour = ($this->hour + 1) > 12 ? $this->hour - 12 : $this->hour;
\r
432 $output .= sprintf("%d", $hour==0 ? 12 : $hour);
\r
435 $output .= Date_Calc::julianDate($this->day,$this->month,$this->year);
\r
438 $output .= sprintf("%02d",$this->month);
\r
441 $output .= sprintf("%02d",$this->minute);
\r
447 $offms = $this->tz->getOffset($this);
\r
448 $direction = $offms >= 0 ? "+" : "-";
\r
449 $offmins = abs($offms) / 1000 / 60;
\r
450 $hours = $offmins / 60;
\r
451 $minutes = $offmins % 60;
\r
452 $output .= sprintf("%s%02d:%02d", $direction, $hours, $minutes);
\r
455 $offms = $this->tz->getRawOffset($this);
\r
456 $direction = $offms >= 0 ? "+" : "-";
\r
457 $offmins = abs($offms) / 1000 / 60;
\r
458 $hours = $offmins / 60;
\r
459 $minutes = $offmins % 60;
\r
460 $output .= sprintf("%s%02d:%02d", $direction, $hours, $minutes);
\r
463 $output .= $this->hour >= 12 ? "pm" : "am";
\r
466 $output .= $this->hour >= 12 ? "PM" : "AM";
\r
469 $hour = ($this->hour + 1) > 12 ? $this->hour - 12 : $this->hour;
\r
470 $output .= sprintf("%02d:%02d:%02d %s", $hour==0 ? 12 : $hour, $this->minute, $this->second, $this->hour >= 12 ? "PM" : "AM");
\r
473 $output .= sprintf("%02d:%02d", $this->hour, $this->minute);
\r
476 $output .= str_replace(',', '.', sprintf("%09f", (float)((float)$this->second + $this->partsecond)));
\r
479 $output .= sprintf("%02d", $this->second);
\r
485 $output .= sprintf("%02d:%02d:%02d", $this->hour, $this->minute, $this->second);
\r
488 $output .= Date_Calc::dayOfWeek($this->day,$this->month,$this->year);
\r
491 $output .= Date_Calc::weekOfYear($this->day,$this->month,$this->year);
\r
494 $output .= substr($this->year,2,2);
\r
497 $output .= $this->year;
\r
500 $output .= $this->tz->inDaylightTime($this) ? $this->tz->getDSTShortName() : $this->tz->getShortName();
\r
506 $output .= $char.$nextchar;
\r
521 * Get this date/time in Unix time() format
\r
523 * Get a representation of this date in Unix time() format. This may only be
\r
524 * valid for dates from 1970 to ~2038.
\r
527 * @return int number of seconds since the unix epoch
\r
531 return $this->getDate(DATE_FORMAT_UNIXTIME);
\r
538 * Sets the time zone of this Date
\r
540 * Sets the time zone of this date with the given
\r
541 * Date_TimeZone object. Does not alter the date/time,
\r
542 * only assigns a new time zone. For conversion, use
\r
546 * @param object Date_TimeZone $tz the Date_TimeZone object to use, if called
\r
547 * with a paramater that is not a Date_TimeZone object, will fall through to
\r
550 function setTZ($tz)
\r
552 if(is_a($tz, 'Date_Timezone')) {
\r
555 $this->setTZbyID($tz);
\r
563 * Sets the time zone of this date with the given time zone id
\r
565 * Sets the time zone of this date with the given
\r
566 * time zone id, or to the system default if the
\r
567 * given id is invalid. Does not alter the date/time,
\r
568 * only assigns a new time zone. For conversion, use
\r
572 * @param string id a time zone id
\r
574 function setTZbyID($id)
\r
576 if (Date_TimeZone::isValidID($id)) {
\r
577 $this->tz = new Date_TimeZone($id);
\r
579 $this->tz = Date_TimeZone::getDefault();
\r
584 // {{{ inDaylightTime()
\r
587 * Tests if this date/time is in DST
\r
589 * Returns true if daylight savings time is in effect for
\r
590 * this date in this date's time zone. See Date_TimeZone::inDaylightTime()
\r
591 * for compatability information.
\r
594 * @return boolean true if DST is in effect for this date
\r
596 function inDaylightTime()
\r
598 return $this->tz->inDaylightTime($this);
\r
605 * Converts this date to UTC and sets this date's timezone to UTC
\r
607 * Converts this date to UTC and sets this date's timezone to UTC
\r
613 if ($this->tz->getOffset($this) > 0) {
\r
614 $this->subtractSeconds(intval($this->tz->getOffset($this) / 1000));
\r
616 $this->addSeconds(intval(abs($this->tz->getOffset($this)) / 1000));
\r
618 $this->tz = new Date_TimeZone('UTC');
\r
625 * Converts this date to a new time zone
\r
627 * Converts this date to a new time zone.
\r
628 * WARNING: This may not work correctly if your system does not allow
\r
629 * putenv() or if localtime() does not work in your environment. See
\r
630 * Date::TimeZone::inDaylightTime() for more information.
\r
633 * @param object Date_TimeZone $tz the Date::TimeZone object for the conversion time zone
\r
635 function convertTZ($tz)
\r
638 if ($this->tz->getOffset($this) > 0) {
\r
639 $this->subtractSeconds(intval(abs($this->tz->getOffset($this)) / 1000));
\r
641 $this->addSeconds(intval(abs($this->tz->getOffset($this)) / 1000));
\r
643 // convert UTC to new timezone
\r
644 if ($tz->getOffset($this) > 0) {
\r
645 $this->addSeconds(intval(abs($tz->getOffset($this)) / 1000));
\r
647 $this->subtractSeconds(intval(abs($tz->getOffset($this)) / 1000));
\r
653 // {{{ convertTZbyID()
\r
656 * Converts this date to a new time zone, given a valid time zone ID
\r
658 * Converts this date to a new time zone, given a valid time zone ID
\r
659 * WARNING: This may not work correctly if your system does not allow
\r
660 * putenv() or if localtime() does not work in your environment. See
\r
661 * Date::TimeZone::inDaylightTime() for more information.
\r
664 * @param string id a time zone id
\r
666 function convertTZbyID($id)
\r
668 if (Date_TimeZone::isValidID($id)) {
\r
669 $tz = new Date_TimeZone($id);
\r
671 $tz = Date_TimeZone::getDefault();
\r
673 $this->convertTZ($tz);
\r
677 // {{{ toUTCbyOffset()
\r
679 function toUTCbyOffset($offset)
\r
681 if ($offset == "Z" || $offset == "+00:00" || $offset == "+0000") {
\r
686 if (preg_match('/([\+\-])(\d{2}):?(\d{2})/', $offset, $regs)) {
\r
687 // convert offset to seconds
\r
688 $hours = (int) isset($regs[2])?$regs[2]:0;
\r
689 $mins = (int) isset($regs[3])?$regs[3]:0;
\r
690 $offset = ($hours * 3600) + ($mins * 60);
\r
692 if (isset($regs[1]) && $regs[1] == "-") {
\r
697 $this->subtractSeconds(intval($offset));
\r
699 $this->addSeconds(intval(abs($offset)));
\r
702 $this->tz = new Date_TimeZone('UTC');
\r
710 // {{{ addSeconds()
\r
713 * Adds a given number of seconds to the date
\r
715 * Adds a given number of seconds to the date
\r
718 * @param int $sec the number of seconds to add
\r
720 function addSeconds($sec)
\r
722 settype($sec, 'int');
\r
724 // Negative value given.
\r
726 $this->subtractSeconds(abs($sec));
\r
730 $this->addSpan(new Date_Span($sec));
\r
737 * Adds a time span to the date
\r
739 * Adds a time span to the date
\r
742 * @param object Date_Span $span the time span to add
\r
744 function addSpan($span)
\r
746 if (!is_a($span, 'Date_Span')) {
\r
750 $this->second += $span->second;
\r
751 if ($this->second >= 60) {
\r
753 $this->second -= 60;
\r
756 $this->minute += $span->minute;
\r
757 if ($this->minute >= 60) {
\r
759 if ($this->hour >= 24) {
\r
760 list($this->year, $this->month, $this->day) =
\r
761 sscanf(Date_Calc::nextDay($this->day, $this->month, $this->year), "%04s%02s%02s");
\r
764 $this->minute -= 60;
\r
767 $this->hour += $span->hour;
\r
768 if ($this->hour >= 24) {
\r
769 list($this->year, $this->month, $this->day) =
\r
770 sscanf(Date_Calc::nextDay($this->day, $this->month, $this->year), "%04s%02s%02s");
\r
774 $d = Date_Calc::dateToDays($this->day, $this->month, $this->year);
\r
777 list($this->year, $this->month, $this->day) =
\r
778 sscanf(Date_Calc::daysToDate($d), "%04s%02s%02s");
\r
779 $this->year = intval($this->year);
\r
780 $this->month = intval($this->month);
\r
781 $this->day = intval($this->day);
\r
785 // {{{ subtractSeconds()
\r
788 * Subtracts a given number of seconds from the date
\r
790 * Subtracts a given number of seconds from the date
\r
793 * @param int $sec the number of seconds to subtract
\r
795 function subtractSeconds($sec)
\r
797 settype($sec, 'int');
\r
799 // Negative value given.
\r
801 $this->addSeconds(abs($sec));
\r
805 $this->subtractSpan(new Date_Span($sec));
\r
809 // {{{ subtractSpan()
\r
812 * Subtracts a time span to the date
\r
814 * Subtracts a time span to the date
\r
817 * @param object Date_Span $span the time span to subtract
\r
819 function subtractSpan($span)
\r
821 if (!is_a($span, 'Date_Span')) {
\r
824 if ($span->isEmpty()) {
\r
828 $this->second -= $span->second;
\r
829 if ($this->second < 0) {
\r
831 $this->second += 60;
\r
834 $this->minute -= $span->minute;
\r
835 if ($this->minute < 0) {
\r
837 if ($this->hour < 0) {
\r
838 list($this->year, $this->month, $this->day) =
\r
839 sscanf(Date_Calc::prevDay($this->day, $this->month, $this->year), "%04s%02s%02s");
\r
842 $this->minute += 60;
\r
845 $this->hour -= $span->hour;
\r
846 if ($this->hour < 0) {
\r
847 list($this->year, $this->month, $this->day) =
\r
848 sscanf(Date_Calc::prevDay($this->day, $this->month, $this->year), "%04s%02s%02s");
\r
852 $d = Date_Calc::dateToDays($this->day, $this->month, $this->year);
\r
855 list($this->year, $this->month, $this->day) =
\r
856 sscanf(Date_Calc::daysToDate($d), "%04s%02s%02s");
\r
857 $this->year = intval($this->year);
\r
858 $this->month = intval($this->month);
\r
859 $this->day = intval($this->day);
\r
866 * Compares two dates
\r
868 * Compares two dates. Suitable for use
\r
869 * in sorting functions.
\r
872 * @param object Date $d1 the first date
\r
873 * @param object Date $d2 the second date
\r
874 * @return int 0 if the dates are equal, -1 if d1 is before d2, 1 if d1 is after d2
\r
876 function compare($d1, $d2)
\r
878 $d1->convertTZ(new Date_TimeZone('UTC'));
\r
879 $d2->convertTZ(new Date_TimeZone('UTC'));
\r
880 $days1 = Date_Calc::dateToDays($d1->day, $d1->month, $d1->year);
\r
881 $days2 = Date_Calc::dateToDays($d2->day, $d2->month, $d2->year);
\r
882 if ($days1 < $days2) return -1;
\r
883 if ($days1 > $days2) return 1;
\r
884 if ($d1->hour < $d2->hour) return -1;
\r
885 if ($d1->hour > $d2->hour) return 1;
\r
886 if ($d1->minute < $d2->minute) return -1;
\r
887 if ($d1->minute > $d2->minute) return 1;
\r
888 if ($d1->second < $d2->second) return -1;
\r
889 if ($d1->second > $d2->second) return 1;
\r
897 * Test if this date/time is before a certain date/time
\r
899 * Test if this date/time is before a certain date/time
\r
902 * @param object Date $when the date to test against
\r
903 * @return boolean true if this date is before $when
\r
905 function before($when)
\r
907 if (Date::compare($this,$when) == -1) {
\r
918 * Test if this date/time is after a certian date/time
\r
920 * Test if this date/time is after a certian date/time
\r
923 * @param object Date $when the date to test against
\r
924 * @return boolean true if this date is after $when
\r
926 function after($when)
\r
928 if (Date::compare($this,$when) == 1) {
\r
939 * Test if this date/time is exactly equal to a certian date/time
\r
941 * Test if this date/time is exactly equal to a certian date/time
\r
944 * @param object Date $when the date to test against
\r
945 * @return boolean true if this date is exactly equal to $when
\r
947 function equals($when)
\r
949 if (Date::compare($this,$when) == 0) {
\r
960 * Determine if this date is in the future
\r
962 * Determine if this date is in the future
\r
965 * @return boolean true if this date is in the future
\r
967 function isFuture()
\r
970 if ($this->after($now)) {
\r
981 * Determine if this date is in the past
\r
983 * Determine if this date is in the past
\r
986 * @return boolean true if this date is in the past
\r
991 if ($this->before($now)) {
\r
999 // {{{ isLeapYear()
\r
1002 * Determine if the year in this date is a leap year
\r
1004 * Determine if the year in this date is a leap year
\r
1007 * @return boolean true if this year is a leap year
\r
1009 function isLeapYear()
\r
1011 return Date_Calc::isLeapYear($this->year);
\r
1015 // {{{ getJulianDate()
\r
1018 * Get the Julian date for this date
\r
1020 * Get the Julian date for this date
\r
1023 * @return int the Julian date
\r
1025 function getJulianDate()
\r
1027 return Date_Calc::julianDate($this->day, $this->month, $this->year);
\r
1031 // {{{ getDayOfWeek()
\r
1034 * Gets the day of the week for this date
\r
1036 * Gets the day of the week for this date (0=Sunday)
\r
1039 * @return int the day of the week (0=Sunday)
\r
1041 function getDayOfWeek()
\r
1043 return Date_Calc::dayOfWeek($this->day, $this->month, $this->year);
\r
1047 // {{{ getWeekOfYear()
\r
1050 * Gets the week of the year for this date
\r
1052 * Gets the week of the year for this date
\r
1055 * @return int the week of the year
\r
1057 function getWeekOfYear()
\r
1059 return Date_Calc::weekOfYear($this->day, $this->month, $this->year);
\r
1063 // {{{ getQuarterOfYear()
\r
1066 * Gets the quarter of the year for this date
\r
1068 * Gets the quarter of the year for this date
\r
1071 * @return int the quarter of the year (1-4)
\r
1073 function getQuarterOfYear()
\r
1075 return Date_Calc::quarterOfYear($this->day, $this->month, $this->year);
\r
1079 // {{{ getDaysInMonth()
\r
1082 * Gets number of days in the month for this date
\r
1084 * Gets number of days in the month for this date
\r
1087 * @return int number of days in this month
\r
1089 function getDaysInMonth()
\r
1091 return Date_Calc::daysInMonth($this->month, $this->year);
\r
1095 // {{{ getWeeksInMonth()
\r
1098 * Gets the number of weeks in the month for this date
\r
1100 * Gets the number of weeks in the month for this date
\r
1103 * @return int number of weeks in this month
\r
1105 function getWeeksInMonth()
\r
1107 return Date_Calc::weeksInMonth($this->month, $this->year);
\r
1111 // {{{ getDayName()
\r
1114 * Gets the full name or abbriviated name of this weekday
\r
1116 * Gets the full name or abbriviated name of this weekday
\r
1119 * @param boolean $abbr abbrivate the name
\r
1120 * @return string name of this day
\r
1122 function getDayName($abbr = false, $length = 3)
\r
1125 return Date_Calc::getWeekdayAbbrname($this->day, $this->month, $this->year, $length);
\r
1127 return Date_Calc::getWeekdayFullname($this->day, $this->month, $this->year);
\r
1132 // {{{ getMonthName()
\r
1135 * Gets the full name or abbriviated name of this month
\r
1137 * Gets the full name or abbriviated name of this month
\r
1140 * @param boolean $abbr abbrivate the name
\r
1141 * @return string name of this month
\r
1143 function getMonthName($abbr = false)
\r
1146 return Date_Calc::getMonthAbbrname($this->month);
\r
1148 return Date_Calc::getMonthFullname($this->month);
\r
1153 // {{{ getNextDay()
\r
1156 * Get a Date object for the day after this one
\r
1158 * Get a Date object for the day after this one.
\r
1159 * The time of the returned Date object is the same as this time.
\r
1162 * @return object Date Date representing the next day
\r
1164 function getNextDay()
\r
1166 $day = Date_Calc::nextDay($this->day, $this->month, $this->year, "%Y-%m-%d");
\r
1167 $date = sprintf("%s %02d:%02d:%02d", $day, $this->hour, $this->minute, $this->second);
\r
1168 $newDate = new Date();
\r
1169 $newDate->setDate($date);
\r
1174 // {{{ getPrevDay()
\r
1177 * Get a Date object for the day before this one
\r
1179 * Get a Date object for the day before this one.
\r
1180 * The time of the returned Date object is the same as this time.
\r
1183 * @return object Date Date representing the previous day
\r
1185 function getPrevDay()
\r
1187 $day = Date_Calc::prevDay($this->day, $this->month, $this->year, "%Y-%m-%d");
\r
1188 $date = sprintf("%s %02d:%02d:%02d", $day, $this->hour, $this->minute, $this->second);
\r
1189 $newDate = new Date();
\r
1190 $newDate->setDate($date);
\r
1195 // {{{ getNextWeekday()
\r
1198 * Get a Date object for the weekday after this one
\r
1200 * Get a Date object for the weekday after this one.
\r
1201 * The time of the returned Date object is the same as this time.
\r
1204 * @return object Date Date representing the next weekday
\r
1206 function getNextWeekday()
\r
1208 $day = Date_Calc::nextWeekday($this->day, $this->month, $this->year, "%Y-%m-%d");
\r
1209 $date = sprintf("%s %02d:%02d:%02d", $day, $this->hour, $this->minute, $this->second);
\r
1210 $newDate = new Date();
\r
1211 $newDate->setDate($date);
\r
1216 // {{{ getPrevWeekday()
\r
1219 * Get a Date object for the weekday before this one
\r
1221 * Get a Date object for the weekday before this one.
\r
1222 * The time of the returned Date object is the same as this time.
\r
1225 * @return object Date Date representing the previous weekday
\r
1227 function getPrevWeekday()
\r
1229 $day = Date_Calc::prevWeekday($this->day, $this->month, $this->year, "%Y-%m-%d");
\r
1230 $date = sprintf("%s %02d:%02d:%02d", $day, $this->hour, $this->minute, $this->second);
\r
1231 $newDate = new Date();
\r
1232 $newDate->setDate($date);
\r
1240 * Returns the year field of the date object
\r
1242 * Returns the year field of the date object
\r
1245 * @return int the year
\r
1247 function getYear()
\r
1249 return (int)$this->year;
\r
1256 * Returns the month field of the date object
\r
1258 * Returns the month field of the date object
\r
1261 * @return int the month
\r
1263 function getMonth()
\r
1265 return (int)$this->month;
\r
1272 * Returns the day field of the date object
\r
1274 * Returns the day field of the date object
\r
1277 * @return int the day
\r
1281 return (int)$this->day;
\r
1288 * Returns the hour field of the date object
\r
1290 * Returns the hour field of the date object
\r
1293 * @return int the hour
\r
1295 function getHour()
\r
1297 return $this->hour;
\r
1301 // {{{ getMinute()
\r
1304 * Returns the minute field of the date object
\r
1306 * Returns the minute field of the date object
\r
1309 * @return int the minute
\r
1311 function getMinute()
\r
1313 return $this->minute;
\r
1317 // {{{ getSecond()
\r
1320 * Returns the second field of the date object
\r
1322 * Returns the second field of the date object
\r
1325 * @return int the second
\r
1327 function getSecond()
\r
1329 return $this->second;
\r
1336 * Set the year field of the date object
\r
1338 * Set the year field of the date object, invalid years (not 0-9999) are set to 0.
\r
1341 * @param int $y the year
\r
1343 function setYear($y)
\r
1345 if ($y < 0 || $y > 9999) {
\r
1356 * Set the month field of the date object
\r
1358 * Set the month field of the date object, invalid months (not 1-12) are set to 1.
\r
1361 * @param int $m the month
\r
1363 function setMonth($m)
\r
1365 if ($m < 1 || $m > 12) {
\r
1368 $this->month = $m;
\r
1376 * Set the day field of the date object
\r
1378 * Set the day field of the date object, invalid days (not 1-31) are set to 1.
\r
1381 * @param int $d the day
\r
1383 function setDay($d)
\r
1385 if ($d > 31 || $d < 1) {
\r
1396 * Set the hour field of the date object
\r
1398 * Set the hour field of the date object in 24-hour format.
\r
1399 * Invalid hours (not 0-23) are set to 0.
\r
1402 * @param int $h the hour
\r
1404 function setHour($h)
\r
1406 if ($h > 23 || $h < 0) {
\r
1414 // {{{ setMinute()
\r
1417 * Set the minute field of the date object
\r
1419 * Set the minute field of the date object, invalid minutes (not 0-59) are set to 0.
\r
1422 * @param int $m the minute
\r
1424 function setMinute($m)
\r
1426 if ($m > 59 || $m < 0) {
\r
1427 $this->minute = 0;
\r
1429 $this->minute = $m;
\r
1434 // {{{ setSecond()
\r
1437 * Set the second field of the date object
\r
1439 * Set the second field of the date object, invalid seconds (not 0-59) are set to 0.
\r
1442 * @param int $s the second
\r
1444 function setSecond($s) {
\r
1445 if ($s > 59 || $s < 0) {
\r
1446 $this->second = 0;
\r
1448 $this->second = $s;
\r
1458 * Local variables:
\r
1461 * c-basic-offset: 4
\r
1462 * c-hanging-comment-ender-p: nil
\r