Source for file Date.class.php
Documentation is available at Date.class.php
* Gumbo Library Framework
* This library is being released under the terms of the New BSD License. A
* copy of the license is packaged with the software (LICENSE.txt). If no
* copy is found, a copy of the license template can be found at:
* http://www.opensource.org/licenses/bsd-license.php
* @copyright Copyright (c) 2007, iBayou, Michael Luster
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @author Michael Luster <mluster79@yahoo.com>
* @link http://sourceforge.net/projects/phpgumbo
* @copyright Copyright (c) 2007, iBayou, Michael Luster
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @author Michael Luster <mluster79@yahoo.com>
* @link http://sourceforge.net/projects/phpgumbo
gumbo_load ("Interface_Date");
class Gumbo_Date implements Gumbo_Interface_Date {
/** @var array $_orig original time stamp [year|month|day] */
/** @var bool $_holiday marks the date as a holiday */
/** @var array $_facts general date facts concerning months */
private $_facts_month =
array (1 =>
array ("name"=>
"January", "days"=>
31),
2 =>
array ("name"=>
"February", "days"=>
28),
3 =>
array ("name"=>
"March", "days"=>
31),
4 =>
array ("name"=>
"April", "days"=>
30),
5 =>
array ("name"=>
"May", "days"=>
31),
6 =>
array ("name"=>
"June", "days"=>
30),
7 =>
array ("name"=>
"July", "days"=>
31),
8 =>
array ("name"=>
"August", "days"=>
31),
9 =>
array ("name"=>
"September", "days"=>
30),
10 =>
array ("name"=>
"October", "days"=>
31),
11 =>
array ("name"=>
"November", "days"=>
30),
12 =>
array ("name"=>
"December", "days"=>
31) );
/** @var array $_facts_day days of the week facts */
private $_facts_day =
array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
/** @var int MONTHS_IN_YEAR */
const MONTHS_IN_YEAR =
12;
/** @var int DAYS_IN_YEAR */
const DAYS_IN_YEAR =
365;
/** @var int DAYS_IN_LEAP_YEAR */
const DAYS_IN_LEAP_YEAR =
366;
/** @var int DAYS_IN_WEEK */
/** @var int WEEKS_IN_YEAR */
const WEEKS_IN_YEAR =
52;
/** @var int HOURS_IN_DAY */
/** @var int HOURS_IN_WEEK */
const HOURS_IN_WEEK =
168;
/** @var int HOURS_IN_YEAR */
const HOURS_IN_YEAR =
8760;
/** @var int HOURS_IN_LEAP_YEAR */
const HOURS_IN_LEAP_YEAR =
8784;
/** @var int MINUTES_IN_HOUR */
const MINUTES_IN_HOUR =
60;
/** @var int MINUTES_IN_DAY */
const MINUTES_IN_DAY =
1440;
/** @var int MINUTES_IN_WEEK */
const MINUTES_IN_WEEK =
10080;
/** @var int MINUTES_IN_YEAR */
const MINUTES_IN_YEAR =
525600;
/** @var int MINUTES_IN_LEAP_YEAR */
const MINUTES_IN_LEAP_YEAR =
527040;
/** @var int SECONDS_IN_MINUTE */
const SECONDS_IN_MINUTE =
60;
/** @var int SECONDS_IN_HOUR */
const SECONDS_IN_HOUR =
360;
/** @var int SECONDS_IN_DAY */
const SECONDS_IN_DAY =
8640;
/** @var int SECONDS_IN_WEEK */
const SECONDS_IN_WEEK =
60480;
/** @var int SECONDS_IN_YEAR */
const SECONDS_IN_YEAR =
3153600;
/** @var int SECONDS_IN_LEAP_YEAR */
const SECONDS_IN_LEAP_YEAR =
3162240;
* @param string|int$stamp full date stamp, or year value
public function __construct ($stamp, $month=
null, $day=
null, $hour=
null, $minute=
null, $seconds=
null) {
* Checks if the current year is a Leap Year
* @throws Gumbo_Exception
} elseif ($year %
400 ==
0) {
} elseif ($year %
100 ==
0) {
$e->setFunction (__METHOD__
);
* Resets the state to the original time stamp
public function reset () {
$this->setDate ($orig ['year'], $orig ['month'], $orig ['day']);
$this->setTime ($orig ['hour'], $orig ['minute'], $orig ['second']);
* Sets the time to the zero hour
* @postcondition $minutes=0
* @postcondition $seconds=0
* Calculates the current Day of the Week
$cent =
floor ($year /
100);
if ($year >
0) { $year--
; }
$wday +=
((($month +
1) *
26) /
10);
$wday +=
$year +
($year /
4);
$wday +=
($cent /
4) -
($cent *
2);
if ($wday <
0) { $wday +=
7; }
if ($wday ==
0) { $wday =
7; }
* Calculates the Day of the Year
for ($x =
1;$x <
13;$x++
) {
if ($x ==
$month) { break; }
* Calculates the Week of the Year
* Increases the Year by number
* @throws Gumbo_Exception
throw
new Gumbo_Exception ("Argument Out of Range, Must Be Greater Than 0");
$e->setFunction (__METHOD__
);
$this->_change ($num, __FUNCTION__
);
* Increases the Month by number.
* @throws Gumbo_Exception
throw
new Gumbo_Exception ("Argument Out of Range, Must Be Greater Than 0");
$e->setFunction (__METHOD__
);
$this->_change ($num, __FUNCTION__
);
* @throws Gumbo_Exception
throw
new Gumbo_Exception ("Argument Out of Range, Must Be Greater Than 0");
$e->setFunction (__METHOD__
);
$this->_change ($num, __FUNCTION__
);
* @throws Gumbo_Exception
throw
new Gumbo_Exception ("Argument Out of Range, Must Be Greater Than 0");
$e->setFunction (__METHOD__
);
$this->_change ($num, __FUNCTION__
);
// checking for a leap year
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
$this->_change ($num, __FUNCTION__
);
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
$this->_change ($num, __FUNCTION__
);
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
$this->_change ($num, __FUNCTION__
);
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
$this->_change ($num, __FUNCTION__
);
if ($this->getYear () ==
0) { return; }
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
$this->_change ($num, __FUNCTION__
);
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
$this->_change ($num, __FUNCTION__
);
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
$this->_change ($num, __FUNCTION__
);
// checking for February leap year
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
$this->_change ($num, __FUNCTION__
);
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
$this->_change ($num, __FUNCTION__
);
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
$this->_change ($num, __FUNCTION__
);
* Sets the original time stamp
* @throws Gumbo_Exception
private function _setOrigStamp ($year, $month, $day, $hour, $minute, $second) {
$this->_orig ['year'] =
$year;
$this->_orig ['month'] =
$month;
$this->_orig ['day'] =
$day;
$this->_orig ['hour'] =
$hour;
$this->_orig ['minute'] =
$minute;
$this->_orig ['second'] =
$second;
$e->setFunction (__METHOD__
);
* Marks the Date as a Holiday
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* Sets the Date values of the object
public function setDate ($year, $month, $day) {
* Sets the Time values of object
public function setTime ($hour, $minutes, $seconds) {
* Sets the Date by a stamp parsed down to yyyymmdd[hhmmss] format
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* Sets the Date by the UNIX Epoch
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* @throws Gumbo_Exception
$this->_year = (int)
$year;
$e->setFunction (__METHOD__
);
* The value can be the numberical month, a three-letter abbreviation of the
* month name, or the full name.
* @precondition if int, 0 < $val < 13
* @param int|string$month
* @throws Gumbo_Exception
if ($month <=
0 ||
$month >=
13) {
$e->setFunction (__METHOD__
);
* @precondition 0 < $val <= 31
* @throws Gumbo_Exception
public function setDay ($day) {
if ($day <=
0 ||
$day >
31) {
// check for maximum day on current month
$num =
$tmp [$this->getMonth ()]['days'];
$this->_day = (int)
$day;
$e->setFunction (__METHOD__
);
* @precondition 0 <= $val < 24
* @throws Gumbo_Exception
if ($hour <
0 ||
$hour >=
24) {
$this->_hour = (int)
$hour;
$e->setFunction (__METHOD__
);
* @precondition 0 <= $val < 60
* @throws Gumbo_Exception
if ($min <
0 ||
$min >=
60) {
$e->setFunction (__METHOD__
);
* @precondition 0 <= $val < 60
* @throws Gumbo_Exception
if ($sec <
0 ||
$sec >=
60) {
$e->setFunction (__METHOD__
);
* Parses the timestamp into Date components
* @param string|int$stamp
private function _parse ($stamp) {
* Changes the particular element by the supplied number
* @param int $num number to increase/decrease
* @param string $method Date method to call
* @throws Gumbo_Exception
private function _change ($num, $method) {
for ($x =
0;$x <
$num;$x++
) {
$e->setFunction (__METHOD__
);
* Returns the original time stamp
* @return array as [year|month|day]
* Returns a Date string in predefined format
* - 0 => yyyy-mm-dd hh:mm:ss (MySQL DATETIME)
* To use more complex combinations, use a combination of the
* getDate() and getTime() methods.
* @param int $format format code
* @throws Gumbo_Exception
public function get ($format=
null) {
if (is_null ($format)) { $format =
0; }
if ($format <
0 ||
$format >
6) {
$e->setFunction (__METHOD__
);
case 1 :
$txt =
$this->getDate (1); break;
case 2 :
$txt =
$this->getDate (2); break;
* Returns a formatted Date string
* - 0 => yyyy-mm-dd (default)
* - 5 => Mon [d]d[suffix], yyyy
* - 6 => Month [d]d[suffix], yyyy
* - 7 => Day, [d]d Mon yyyy
* The method also uses the date() and strftime() functions. The format
* would have to be a string representing values the function will parse.
* If the percent sign (%) is found, it will use strftime(), else it will
* @param string|int$format pre-defined format code or string stamp
* @throws Gumbo_Exception
public function getDate ($format=
null) {
if (is_null ($format)) { $format =
0; }
if ($format <
0 ||
$format >
7) {
$e->setFunction (__METHOD__
);
return date ($format, $stamp);
* Returns a formatted Time string
* - 0 => hh:mm:ss (default)
* - 9 => hh:mm:ss [am|pm]
* - 11 => [h]h:mm:ss [am|pm]
* @param int $format pre-defined time format
* @throws Gumbo_Exception
public function getTime ($format=
null) {
if (is_null ($format)) { $format =
0; }
if ($format <
0 ||
$format >
11) {
$e->setFunction (__METHOD__
);
if ($format ==
1) { return $this->getHour (1); }
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* Returns the Year requested format
* @throws Gumbo_Exception
public function getYear ($format=
null) {
if (is_null ($format)) { $format =
0; }
if ($format <
0 ||
$format >
1) {
$e->setFunction (__METHOD__
);
* Returns the Month in the requested format
* 2 -> Mon (abbreviation)
* @throws Gumbo_Exception
public function getMonth ($format=
null) {
if (is_null ($format)) { $format =
0; }
if ($format <
0 ||
$format >
3) {
$e->setFunction (__METHOD__
);
if ($format ==
1) { if (strlen ($this->_month) ==
1) { return "0" .
$this->_month; } else { $format =
0; } }
* Returns the Day in the requested format
* - 2 => ## (numerical day of week)
* - 3 => Day (abbreviation)
* - 5 => ## (day of year)
* @throws Gumbo_Exception
public function getDay ($format=
null) {
if (is_null ($format)) { $format =
0; }
if ($format <
0 ||
$format >
5) {
$e->setFunction (__METHOD__
);
if ($format ==
1) { if (strlen ($this->_day) ==
1) { return "0" .
$this->_day; } else { $format =
0; } }
* Returns the Hour in the requested format
* - 0 => [h]h (24-hour format)
* - 1 => hh (24-hour format)
* - 2 => [h]h (12-hour format)
* - 3 => hh (12-hour format)
* @throws Gumbo_Exception
public function getHour ($format=
null) {
if (is_null ($format)) { $format =
0; }
if ($format <
0 ||
$format >
3) {
$e->setFunction (__METHOD__
);
return "0" .
$this->_hour;
if ($hr >
12) { $hr -=
12; }
if ($hr ==
0) { $hr =
12; }
if ($format ==
2) { return $hr; }
* Returns the Minutes in the requested format
* @throws Gumbo_Exception
if (is_null ($format)) { $format =
0; }
if ($format <
0 ||
$format >
1) {
$e->setFunction (__METHOD__
);
* Returns the Seconds in the requested format
* @throws Gumbo_Exception
if (is_null ($format)) { $format =
0; }
if ($format <
0 ||
$format >
1) {
$e->setFunction (__METHOD__
);
* Returns an array of factual months
* Returns the total days within a month
* @throws Gumbo_Exception
if ($month <=
0 ||
$month >=
13) {
$e->setFunction (__METHOD__
);
* Returns if the current day is on the weekend
* Returns if the current day is on the weekday
* Returns if the current day is a holiday
return ($this->getDay (2) ==
0);
return ($this->getDay (2) ==
1);
return ($this->getDay (2) ==
2);
return ($this->getDay (2) ==
3);
return ($this->getDay (2) ==
4);
return ($this->getDay (2) ==
5);
return ($this->getDay (2) ==
6);
public function isMay () {
* Returns the difference from the supplied Date
* @param Gumbo_Interface_Date $date
* @return array [when|year|month|day|hour|minute|second]
public function diff (Gumbo_Interface_Date $date) {
$diff =
array ("when"=>
"equal", "year"=>
0, "month"=>
0, "day"=>
0, "hour"=>
0, "minute"=>
0, "second"=>
0);
if ($this->get (5) >
$obj->get (5)) { $diff ['when'] =
"before"; }
if ($this->get (5) <
$obj->get (5)) { $diff ['when'] =
"after"; }
if ($obj->get (5) >
$this->get (5)) { $func =
"prevYear"; }
while ($obj->getYear () !=
$this->getYear ()) {
if ($obj->get (5) >
$this->get (5)) { $func =
"prevMonth"; }
while ($obj->getMonth () !=
$this->getMonth ()) {
if ($obj->get (5) >
$this->get (5)) { $func =
"prevDay"; }
while ($obj->getDay () !=
$this->getDay ()) {
if ($obj->get (5) >
$this->get (5)) { $func =
"prevHour"; }
while ($obj->getHour () !=
$this->getHour ()) {
if ($obj->get (5) >
$this->get (5)) { $func =
"prevMinute"; }
while ($obj->getMinute () !=
$this->getMinute ()) {
if ($obj->get (5) >
$this->get (5)) { $func =
"prevSecond"; }
while ($obj->getSecond () !=
$this->getSecond ()) {
* Returns the number of Years difference
* @param int|Gumbo_Interface_Date$val
* Returns the number of Months difference
* @param Gumbo_Interface_Date $val
public function diffMonth (Gumbo_Interface_Date $val) {
if ($this->getDate (1) >
$obj->getDate (1)) {
while ($this->getDate (1) >
$obj->getDate (1)) {
while ($this->getDate (1) <
$obj->getDate (1)) {
* Returns the number of Weeks difference
* @param Gumbo_Interface_Date $val
public function diffWeek (Gumbo_Interface_Date $val) {
if ($this->getDate (2) >
$obj->getDate (2)) {
while ($this->getDate (2) >
$obj->getDate (2)) {
while ($this->getDate (2) <
$obj->getDate (2)) {
* Returns the number of Days difference
* @param Gumbo_Interface_Date $val
public function diffDay (Gumbo_Interface_Date $val) {
if ($this->getDate (2) >
$obj->getDate (2)) {
while ($this->getDate (2) >
$obj->getDate (2)) {
while ($this->getDate (2) <
$obj->getDate (2)) {
* Returns the AM/PM time value
* @param bool $in_caps returns value in caps
* @throws Gumbo_Exception
public function meridien ($in_caps=
false) {
$e->setFunction (__METHOD__
);
if ($this->getHour () >
11) { $tmp =
"pm"; }