cvs: php4 /pear/Date Calendar.php
| From: | Monte Ohrt | Date: | Fri, 13 Oct 2000 18:49:18 +0000 |
| Subject: | cvs: php4 /pear/Date Calendar.php | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-2046@lists.php.net to get a copy of this message | ||
mohrt Fri Oct 13 11:49:18 2000 EDT
Added files:
/php4/pear/Date Calendar.php
Log:
initial commit
Index: php4/pear/Date/Calendar.php
+++ php4/pear/Date/Calendar.php
<?php
/**
* Date_Calendar is a calendar class used to calculate and
* manipulate calendar dates and retrieve dates in a calendar
* format. It does not rely on 32-bit system date stamps, so
* you can display calendars and compare dates that date
* pre 1970 and post 2038.
*
* This source file is subject to version 2.02 of the PHP license,
* that is bundled with this package in the file LICENSE, and is
* available at through the world-wide-web at
* http://www.php.net/license/2_02.txt.
* If you did not receive a copy of the PHP license and are unable to
* obtain it through the world-wide-web, please send a note to
* license@php.net so we can mail you a copy immediately.
*
* Copyright (c) 1999, 2000 ispi
*
* @access public
*
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*/
class Date_Calendar {
/**
* Determine what day is the beginning of the calendar week.
* 0=Sunday, 1=Monday
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @var int [$begin_weekday]
*/
var $begin_weekday = 0; // first day of week.
/**
* Returns the current local date. NOTE: This function
* retrieves the local date using strftime(), which may
* or may not be 32-bit safe on your system.
*
* @param string the strftime() format to return the date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string the current date in specified format
*/
function date_now($format="%Y%m%d")
{
return(strftime($format,time()));
} // end func date_now
/**
* Returns true for valid date, false for invalid date.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string day in format DD
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return boolean true/false
*/
function is_valid_date($year, $month, $day)
{
if(empty($year) || empty($month) || empty($day))
return false;
// must be digits only
if(preg_match("/\D/",$year))
return false;
if(preg_match("/\D/",$month))
return false;
if(preg_match("/\D/",$day))
return false;
if($year < 0 || $year > 9999)
return false;
if($month < 1 || $month > 12)
return false;
if($day < 1 || $day > 31 || $day > $this->days_in_month($month))
return false;
return true;
} // end func is_valid_date
function is_leap_year($year="")
{
if(empty($year))
$year = $this->date_now("%Y");
if(strlen($year) != 4)
return false;
if(preg_match("/\D/",$year))
return false;
return (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0);
} // end func is_leap_year
/**
* Determines if given date is a future date from now.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string day in format DD
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return boolean true/false
*/
function is_future_date($year,$month,$day)
{
$this_year = $this->date_now("%Y");
$this_month = $this->date_now("%m");
$this_day = $this->date_now("%d");
if($year > $this_year)
return true;
elseif($year == $this_year)
if($month > $this_month)
return true;
elseif($month == $this_month)
if($day > $this_day)
return true;
return false;
} // end func is_future_date
/**
* Determines if given date is a past date from now.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string day in format DD
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return boolean true/false
*/
function is_past_date($year,$month,$day)
{
$this_year = $this->date_now("%Y");
$this_month = $this->date_now("%m");
$this_day = $this->date_now("%d");
if($year < $this_year)
return true;
elseif($year == $this_year)
if($month < $this_month)
return true;
elseif($month == $this_month)
if($day < $this_day)
return true;
return false;
} // end func is_past_date
/**
* Returns day of week for given date, 0=Sunday
*
* @param string year in format CCYY, default is current local year
* @param string month in format MM, default is current local month
* @param string day in format DD, default is current local day
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return int $weekday_number
*/
function day_of_week($year="",$month="",$day="")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
if($month > 2)
$month -= 2;
else
{
$month += 10;
$year--;
}
$day = ( floor((13 * $month - 1) / 5) +
$day + ($year % 100) +
floor(($year % 100) / 4) +
floor(($year / 100) / 4) - 2 *
floor($year / 100) + 77);
$weekday_number = (($day - 7 * floor($day / 7)));
return $weekday_number;
} // end func day_of_week
/**
* Returns week of the year, first Sunday is first day of first week
*
* @param string year in format CCYY
* @param string month in format MM
* @param string day in format DD
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return integer $week_number
*/
function week_of_year($year,$month,$day)
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$week_year = $year - 1501;
$week_day = $week_year * 365 + floor($week_year / 4) - 29872 + 1
- floor($week_year / 100) + floor(($week_year - 300) / 400);
$week_number =
floor(($this->julian_date($year,$month,$day) + floor(($week_day + 4) % 7)) / 7);
return $week_number;
} // end func week_of_year
/**
* Returns number of days since 31 December of year before given date.
*
* @param string year in format CCYY, default is current local year
* @param string month in format MM, default is current local month
* @param string day in format DD, default is current local day
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return int $julian
*/
function julian_date($year="",$month="",$day="")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$days = array(0,31,59,90,120,151,181,212,243,273,304,334);
$julian = ($days[$month - 1] + $day);
if($month > 2 && $this->is_leap_year($year))
$julian++;
return($julian);
} // end func julian_date
/**
* Returns quarter of the year for given date
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string day in format DD, default current local day
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return int $year_quarter
*/
function quarter_of_year($year="",$month="",$day="")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$year_quarter = (intval(($month - 1) / 3 + 1));
return $year_quarter;
} // end func quarter_of_year
/**
* Returns date of begin of next month of given date.
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string day in format DD, default current local day
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string date in given format
*/
function
begin_of_next_month($year="",$month="",$day="",$format="%Y%m%d")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
if($month < 12)
{
$month++;
$day=1;
}
else
{
$year++;
$month=1;
$day=1;
}
return $this->date_format($year,$month,$day,$format);
} // end func begin_of_next_month
/**
* Returns date of the last day of next month of given date.
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string day in format DD, default current local day
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string date in given format
*/
function
end_of_next_month($year="",$month="",$day="",$format="%Y%m%d")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$day = $this->days_in_month($month);
if($month < 12)
{
$month++;
}
else
{
$year++;
$month=1;
}
return $this->date_format($year,$month,$day,$format);
} // end func end_of_next_month
/**
* Returns date of the first day of previous month of given date.
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string day in format DD, default current local day
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string date in given format
*/
function
begin_of_prev_month($year="",$month="",$day="",$format="%Y%m%d")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
if($month > 1)
{
$month--;
$day=1;
}
else
{
$year--;
$month=12;
$day=1;
}
return $this->date_format($year,$month,$day,$format);
} // end func begin_of_prev_month
/**
* Returns date of the last day of previous month for given date.
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string day in format DD, default current local day
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string date in given format
*/
function
end_of_prev_month($year="",$month="",$day="",$format="%Y%m%d")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$day = $this->days_in_month($date);
if($month > 1)
{
$month--;
}
else
{
$year--;
$month=12;
}
return $this->date_format($year,$month,$day,$format);
} // end func end_of_prev_month
/**
* Returns date of the next weekday of given date,
* skipping from Friday to Monday.
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string day in format DD, default current local day
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string date in given format
*/
function
next_weekday($year="",$month="",$day="",$format="%Y%m%d")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$days = $this->date_to_days($year,$month,$day);
if($this->day_of_week($year,$month,$day) == 5)
$days += 3;
elseif($this->day_of_week($year,$month,$day) == 6)
$days += 2;
else
$days += 1;
return($this->days_to_date($days,$format));
} // end func next_weekday
/**
* Returns date of the previous weekday,
* skipping from Monday to Friday.
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string day in format DD, default current local day
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string date in given format
*/
function
prev_weekday($year="",$month="",$day="",$format="%Y%m%d")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$days = $this->date_to_days($year,$month,$day);
if($this->day_of_week($year,$month,$day) == 1)
$days -= 3;
elseif($this->day_of_week($year,$month,$day) == 0)
$days -= 2;
else
$days -= 1;
return($this->days_to_date($days,$format));
} // end func prev_weekday
/**
* Returns date of day after given date.
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string day in format DD, default current local day
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string date in given format
*/
function
next_day($year="",$month="",$day="",$format="%Y%m%d")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$days = $this->date_to_days($year,$month,$day);
return($this->days_to_date($days + 1,$format));
} // end func next_day
/**
* Returns date of day before given date.
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string day in format DD, default current local day
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string date in given format
*/
function
prev_day($year="",$month="",$day="",$format="%Y%m%d")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$days = $this->date_to_days($year,$month,$day);
return($this->days_to_date($days - 1,$format));
} // end func prev_day
/**
* Sets century for 2 digit year.
* 51-99 is 19, else 20
*
* @param string 2 digit year
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string 4 digit year
*/
function default_century($year)
{
if(strlen($year) == 1)
$year = "0$year";
if($year > 50)
return( "19$year" );
else
return( "20$year" );
} // end func default_century
/**
* Returns number of days between two given dates.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string day in format DD
* @param string year in format CCYY
* @param string month in format MM
* @param string day in format DD
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return int absolute number of days between dates,
* -1 if there is an error.
*/
function date_diff($year1,$month1,$day1,$year2,$month2,$day2)
{
if(!$this->is_valid_date($year1,$month1,$day1))
return -1;
if(!$this->is_valid_date($year2,$month2,$day2))
return -1;
return(abs(($this->date_to_days($year1,$month1,$day1))
- ($this->date_to_days($year2,$month2,$day2))));
} // end func date_diff
/**
* Find the number of days in the given month.
*
* @param string month in format MM, default current local month
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return int number of days
*/
function days_in_month($month="")
{
if(empty($month))
$month = $this->date_now("%m");
if($month == 2)
{
if($this->is_leap_year($year))
return 29;
else
return 28;
}
elseif($month == 4 or $month == 6 or $month == 9 or $month == 11)
return 30;
else
return 31;
} // end func days_in_month
/**
* Find the day of the week for the first of the month of given date.
*
* @param string year in format CCYY, default to current local year
* @param string month in format MM, default to current local month
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return int number of weekday for the first day, 0=Sunday
*/
function first_of_month_weekday($year="",$month="")
{
if(empty($year) || empty($month))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
}
return($this->day_of_week($year,$month,"01"));
} // end func first_of_month_weekday
/**
* Return date of first day of month of given date.
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string date in given format
*/
function begin_of_month($year="",$month="",$format="%Y%m%d")
{
if(empty($year) || empty($month))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
}
return($this->date_format($year,$month,"01",$format));
} // end of func begin_of_month
/**
* Find the month day of the beginning of week for given date,
* using $this->begin_weekday. (can return weekday of prev month.)
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string day in format DD, default current local day
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string date in given format
*/
function
begin_of_week($year="",$month="",$day="",$format="%Y%m%d")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$this_weekday = $this->day_of_week($year,$month,$day);
$begin_of_week = ($this->date_to_days($year,$month,$day)
- ($this_weekday - $this->begin_weekday));
return($this->days_to_date($begin_of_week,$format));
} // end of func begin_of_week
/**
* Find the month day of the end of week for given date,
* using $this->begin_weekday. (can return weekday
* of following month.)
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string day in format DD, default current local day
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string date in given format
*/
function
end_of_week($year="",$month="",$day="",$format="%Y%m%d")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$this_weekday = $this->day_of_week($year,$month,$day);
$last_day_of_week = ($this->date_to_days($year,$month,$day)
+ (6 - $this_weekday + $this->begin_weekday));
return($this->days_to_date($last_day_of_week,$format));
} // end func end_of_week
/**
* Find the month day of the beginning of week after given date,
* using $this->begin_weekday. (can return weekday of prev month.)
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string day in format DD, default current local day
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string date in given format
*/
function
begin_of_next_week($year="",$month="",$day="",$format="%Y%m%d")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$date =
$this->days_to_date($this->date_to_days($year,$month,$day+7),"%Y%m%d");
$next_week_year = substr($date,0,4);
$next_week_month = substr($date,4,2);
$next_week_day = substr($date,6,2);
$this_weekday = $this->day_of_week($next_week_year,$next_week_month,$next_week_day);
$begin_of_week = ($this->date_to_days($next_week_year,$next_week_month,$next_week_day)
- ($this_weekday - $this->begin_weekday));
return($this->days_to_date($begin_of_week,$format));
} // end func begin_of_next_week
/**
* Find the month day of the beginning of week before given date,
* using $this->begin_weekday. (can return weekday of prev month.)
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string day in format DD, default current local day
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string date in given format
*/
function
begin_of_prev_week($year="",$month="",$day="",$format="%Y%m%d")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$date =
$this->days_to_date($this->date_to_days($year,$month,$day-7),"%Y%m%d");
$next_week_year = substr($date,0,4);
$next_week_month = substr($date,4,2);
$next_week_day = substr($date,6,2);
$this_weekday = $this->day_of_week($next_week_year,$next_week_month,$next_week_day);
$begin_of_week = ($this->date_to_days($next_week_year,$next_week_month,$next_week_day)
- ($this_weekday - $this->begin_weekday));
return($this->days_to_date($begin_of_week,$format));
} // end func begin_of_prev_week
/**
* Return an array with days in week
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string day in format DD, default current local day
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return array $week[$weekday]
*/
function
get_calendar_week($year="",$month="",$day="",$format="%Y%m%d")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$week_array = array();
// date for the column of week
$curr_day = ($this->date_to_days($year,$month,$day)
- (($this->day_of_week($year,$month,$day) - $this->begin_weekday)));
for($counter=0; $counter <= 6; $counter++)
{
$week_array[$counter] = $this->days_to_date($curr_day,$format);
$curr_day++;
}
return $week_array;
} // end func get_calendar_week
/**
* Return a set of arrays to construct a calendar month for
* the given date.
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return array $month[$row][$col]
*/
function get_calendar_month($year="",$month="",$format="%Y%m%d")
{
if(empty($year) || empty($month))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
}
$month_array = array();
// date for the first row, first column of calendar month
$curr_day = ($this->date_to_days($year,$month,"01")
- $this->first_of_month_weekday($year,$month,"01") +
$this->begin_weekday);
// number of days in this month
$days_in_month = $this->days_in_month($month);
for($row_counter=0; $row_counter <= 5; $row_counter++)
{
for($column_counter=0; $column_counter <= 6; $column_counter++)
{
$month_array[$row_counter][$column_counter] =
$this->days_to_date($curr_day,$format);
$curr_day++;
}
}
return $month_array;
} // end func get_calendar_month
/**
* Return a set of arrays to construct a calendar year for
* the given date.
*
* @param string year in format CCYY, default current local year
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return array $year[$month][$row][$col]
*/
function get_calendar_year($year="",$format="%Y%m%d")
{
if(empty($year))
$year = $this->date_now("%Y");
$year_array = array();
for($curr_month=0; $curr_month <=11; $curr_month++)
{
$year_array[$curr_month] =
$this->get_calendar_month($year,sprintf("%02d",$curr_month+1),$format);
/* for ($x=0; $x< count($year_array[$curr_month]); $x++)
{
for($y=0; $y< count($year_array[$curr_month][$x]); $y++)
{
echo "DEBUG:
$x,$y:".$year_array[$curr_month][$x][$y]."<br>\n";
}
} */
}
return $year_array;
} // end func get_calendar_year
/**
* Converts a date to number of days since a
* distant unspecified epoch.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string day in format DD
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return integer number of days
*/
function date_to_days($year,$month,$day)
{
$century = substr($year,0,2);
$year = substr($year,2,2);
if($month > 2)
$month -= 3;
else
{
$month += 9;
if($year)
$year--;
else
{
$year = 99;
$century --;
}
}
return ( floor(( 146097 * $century) / 4 ) +
floor(( 1461 * $year) / 4 ) +
floor(( 153 * $month + 2) / 5 ) +
$day + 1721119);
} // end func date_to_days
/**
* Converts number of days to a distant unspecified epoch.
*
* @param int number of days
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string date in specified format
*/
function days_to_date($days,$format="%Y%m%d")
{
$days -= 1721119;
$century = floor(( 4 * $days - 1) / 146097);
$days = floor(4 * $days - 1 - 146097 * $century);
$day = floor($days / 4);
$year = floor(( 4 * $day + 3) / 1461);
$day = floor(4 * $day + 3 - 1461 * $year);
$day = floor(($day + 4) / 4);
$month = floor(( 5 * $day - 3) / 153);
$day = floor(5 * $day - 3 - 153 * $month);
$day = floor(($day + 5) / 5);
if($month < 10)
$month +=3;
else
{
$month -=9;
if($year++ == 99)
{
$year = 0;
$century++;
}
}
$century = sprintf("%02d",$century);
$year = sprintf("%02d",$year);
return($this->date_format($century.$year,$month,$day,$format));
} // end func days_to_date
/**
* Calculates the date of the Nth weekday of the month,
* such as the second Saturday of January 2000.
*
* @param string occurance: 1=first, 2=second, 3=third, etc.
* @param string day_of_week: 0=Sunday, 1=Monday, etc.
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string date in given format
*/
function N_weekday_of_month($occurance,$day_of_week,$year,$month,$format="%Y%m%d")
{
$year = sprintf("%04d",$year);
$month = sprintf("%02d",$month);
$DOW1day = sprintf("%02d",(($occurance - 1) * 7 + 1));
$DOW1 = $this->day_of_week($year.$month.$DOW1day);
$wdate = ($occurance - 1) * 7 + 1 +
(7 + $day_of_week - $DOW1) % 7;
if( $wdate > $this->days_in_month($date))
return -1;
else
return($this->date_format($year,$month,$wdate,$format));
} // end func N_weekday_of_month
/**
* Calculates the first Sunday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function first_sunday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(1,0,$year,$month,$format);
} // end func first_sunday_of_month
/**
* Calculates the second Sunday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function second_sunday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(2,0,$year,$month,$format);
} // end func second_sunday_of_month
/**
* Calculates the third Sunday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function third_sunday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(3,0,$year,$month,$format);
} // end func third_sunday_of_month
/**
* Calculates the fourth Sunday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function fourth_sunday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(4,0,$year,$month,$format);
} // end func fourth_sunday_of_month
/**
* Calculates the fifth Sunday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function fifth_sunday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(5,0,$year,$month,$format);
} // end func fifth_sunday_of_month
/**
* Calculates the first Monday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function first_monday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(1,1,$year,$month,$format);
} // end func first_monday_of_month
/**
* Calculates the second Monday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function second_monday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(2,1,$year,$month,$format);
} // end func second_monday_of_month
/**
* Calculates the third Monday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function third_monday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(3,1,$year,$month,$format);
} // end func third_monday_of_month
/**
* Calculates the fourth Monday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function fourth_monday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(4,1,$year,$month,$format);
} // end func fourth_monday_of_month
/**
* Calculates the fifth Monday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function fifth_monday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(5,1,$year,$month,$format);
} // end func fifth_monday_of_month
/**
* Calculates the first Tuesday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function first_tuesday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(1,2,$year,$month,$format);
} // end func first_tuesday_of_month
/**
* Calculates the second Tuesday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function second_tuesday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(2,2,$year,$month,$format);
} // end func second_tuesday_of_month
/**
* Calculates the third Tuesday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function third_tuesday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(3,2,$year,$month,$format);
} // end func third_tuesday_of_month
/**
* Calculates the fourth Tuesday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function fourth_tuesday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(4,2,$year,$month,$format);
} // end func fourth_tuesday_of_month
/**
* Calculates the fifth Tuesday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function fifth_tuesday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(5,2,$year,$month,$format);
} // end func fifth_tuesday_of_month
/**
* Calculates the first Wednesday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function first_wednesday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(1,3,$year,$month,$format);
} // end func first_wednesday_of_month
/**
* Calculates the second Wednesday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function second_wednesday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(2,3,$year,$month,$format);
} // end func second_wednesday_of_month
/**
* Calculates the third Wednesday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function third_wednesday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(3,3,$year,$month,$format);
} // end func third_wednesday_of_month
/**
* Calculates the fourth Wednesday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function fourth_wednesday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(4,3,$year,$month,$format);
} // end func fourth_wednesday_of_month
/**
* Calculates the fifth Wednesday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function fifth_wednesday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(5,3,$year,$month,$format);
} // end func fifth_wednesday_of_month
/**
* Calculates the first Thursday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function first_thursday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(1,4,$year,$month,$format);
} // end func first_thursday_of_month
/**
* Calculates the second Thursday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function second_thursday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(2,4,$year,$month,$format);
} // end func second_thursday_of_month
/**
* Calculates the third Thursday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function third_thursday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(3,4,$year,$month,$format);
} // end func third_thursday_of_month
/**
* Calculates the fourth Thursday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function fourth_thursday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(4,4,$year,$month,$format);
} // end func fourth_thursday_of_month
/**
* Calculates the fifth Thursday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function fifth_thursday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(5,4,$year,$month,$format);
} // end func fifth_thursday_of_month
/**
* Calculates the first Friday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function first_friday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(1,5,$year,$month,$format);
} // end func first_friday_of_month
/**
* Calculates the second Friday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function second_friday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(2,5,$year,$month,$format);
} // end func second_friday_of_month
/**
* Calculates the third Friday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function third_friday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(3,5,$year,$month,$format);
} // end func third_friday_of_month
/**
* Calculates the fourth Friday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function fourth_friday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(4,5,$year,$month,$format);
} // end func fourth_friday_of_month
/**
* Calculates the fifth Friday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function fifth_friday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(5,5,$year,$month,$format);
} // end func fifth_friday_of_month
/**
* Calculates the first Saturday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function first_saturday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(1,6,$year,$month,$format);
} // end func first_saturday_of_month
/**
* Calculates the second Saturday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function second_saturday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(2,6,$year,$month,$format);
} // end func second_saturday_of_month
/**
* Calculates the third Saturday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function third_saturday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(3,6,$year,$month,$format);
} // end func third_saturday_of_month
/**
* Calculates the fourth Saturday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function fourth_saturday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(4,6,$year,$month,$format);
} // end func fourth_saturday_of_month
/**
* Calculates the fifth Saturday of the given month.
*
* @param string year in format CCYY
* @param string month in format MM
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @see N_weekday_of_month()
*
* @return string date in given format
*/
function fifth_saturday_of_month($year,$month,$format)
{
return $this->N_weekday_of_month(5,6,$year,$month,$format);
} // end func fifth_saturday_of_month
/**
* Formats the date in the given format, much like
* strfmt(). This function is used to alleviate the
* problem with 32-bit numbers for dates pre 1970
* or post 2038, as strfmt() has on most systems.
* Most of the formatting options are compatible.
*
* formatting options:
*
* %a abbreviated weekday name (Sun, Mon, Tue)
* %A full weekday name (Sunday, Monday, Tuesday)
* %b abbreviated month name (Jan, Feb, Mar)
* %B full month name (January, February, March)
* %d day of month (range 00 to 31)
* %e day of month, single digit (range 0 to 31)
* %E number of days since unspecified epoch (integer)
* (%E is useful for passing a date in a URL as
* an integer value. Then simply use
* days_to_date() to convert back to a date.)
* %j day of year (range 001 to 366)
* %m month as decimal number (range 1 to 12)
* %n newline character (\n)
* %t tab character (\t)
* %w weekday as decimal (0 = Sunday)
* %U week number of current year, first sunday as first week
* %y year as decimal (range 00 to 99)
* %Y year as decimal including century (range 0000 to 9999)
* %% literal '%'
*
* @param string year in format CCYY
* @param string month in format MM
* @param string day in format DD
* @param string format for returned date
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string date in given format
*/
function date_format($year,$month,$day,$format)
{
if(!$this->is_valid_date($year,$month,$day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$output = "";
for($strpos = 0; $strpos < strlen($format); $strpos++)
{
$char = substr($format,$strpos,1);
if($char == "%")
{
$nextchar = substr($format,$strpos + 1,1);
switch($nextchar)
{
case "a":
$output .= $this->get_weekday_abbrname($year,$month,$day);
break;
case "A":
$output .= $this->get_weekday_fullname($year,$month,$day);
break;
case "b":
$output .= $this->get_month_abbrname($month);
break;
case "B":
$output .= $this->get_month_fullname($month);
break;
case "d":
$output .= sprintf("%02d",$day);
break;
case "e":
$output .= $day;
break;
case "E":
$output .= $this->date_to_days($year,$month,$day);
break;
case "j":
$output .= $this->julian_date($year,$month,$day);
break;
case "m":
$output .= sprintf("%02d",$month);
break;
case "n":
$output .= "\n";
break;
case "t":
$output .= "\t";
break;
case "w":
$output .= $this->day_of_week($year,$month,$day);
break;
case "U":
$output .= $this->week_of_year($year,$month,$day);
break;
case "y":
$output .= substr($year,2,2);
break;
case "Y":
$output .= $year;
break;
case "%":
$output .= "%";
break;
default:
$output .= $char.$nextchar;
}
$strpos++;
}
else
{
$output .= $char;
}
}
return $output;
} // end func date_format
/**
* Returns the current local year in format CCYY
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string year in format CCYY
*/
function get_year()
{
return $this->date_now("%Y");
} // end func get_year
/**
* Returns the current local month in format MM
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string month in format MM
*/
function get_month()
{
return $this->date_now("%m");
} // end func get_month
/**
* Returns the current local day in format DD
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string day in format DD
*/
function get_day()
{
return $this->date_now("%d");
} // end func get_day
/**
* Returns the full month name for the given month
*
* @param string month in format MM
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string full month name
*/
function get_month_fullname($month)
{
if(empty($month))
$month = $this->date_now("%m");
$month_names =
array("January","February","March","April","May","June",
"July","August","September","October","November","December");
return $month_names[($month - 1)];
} // end func get_month_fullname
/**
* Returns the abbreviated month name for the given month
*
* @param string month in format MM
* @param int optional length of abbreviation, default is 3
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string abbreviated month name
*/
function get_month_abbrname($month,$length=3)
{
if(empty($month))
$month = $this->date_now("%m");
$month_names =
array("January","February","March","April","May","June",
"July","August","September","October","November","December");
return substr($month_names[($month - 1)],0,$length);
} // end func get_month_abbrname
/**
* Returns the full weekday name for the given date
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string day in format DD, default current local day
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string full month name
*/
function get_weekday_fullname($year="",$month="",$day="")
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$weekday_names = array("Sunday","Monday","Tuesday",
"Wednesday","Thursday","Friday","Saturday");
$weekday = $this->day_of_week($year,$month,$day);
return $weekday_names[$weekday];
} // end func get_weekday_fullname
/**
* Returns the abbreviated weekday name for the given date
*
* @param string year in format CCYY, default current local year
* @param string month in format MM, default current local month
* @param string day in format DD, default current local day
* @param int optional length of abbreviation, default is 3
*
* @access public
* @version 1.0
* @author Monte Ohrt <monte@ispi.net>
*
* @return string full month name
*/
function
get_weekday_abbrname($year="",$month="",$day="",$length=3)
{
if(empty($year) || empty($month) || empty($day))
{
$year = $this->date_now("%Y");
$month = $this->date_now("%m");
$day = $this->date_now("%d");
}
$weekday_names = array("Sunday","Monday","Tuesday",
"Wednesday","Thursday","Friday","Saturday");
$weekday = $this->day_of_week($year,$month,$date);
return substr($weekday_names[$weekday],0,$length);
} // end func get_weekday_fullname
} // end class Date_calendar
?>