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