// The constant telling us what day starts the week. Monday (1) is the // international standard. Redefine this to 0 if you want weeks to // begin on Sunday. define('DATE_CALC_BEGIN_WEEKDAY', 1);
/** * Date_Calc 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.2.4 * @author Monte Ohrt <monte@ispi.net> */
class Date_Calc {
/** * 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 * * @return string the current date in specified format */
function dateNow($format="%Y%m%d") { return(strftime($format,time()));
} // end func dateNow
/** * 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 * * @return boolean true/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;
/** * 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 * * @return boolean true/false */
/** * 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 * * @return boolean true/false */
/** * 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 * * @return int $weekday_number */
/** * 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 * * @return integer $week_number */
/** * 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 * * @return int $julian */
/** * 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 * * @return int $year_quarter */
/** * 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 * * @return string date in given format */
/** * 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 * * @return string date in given format */
/** * 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 * * @return string date in given format */
/** * 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 * * @return string date in given format */
/** * 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 * * @return string date in given format */
/** * 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 * * @return string date in given format */
/** * Returns date of the next specific day of the week * from the given date. * * @param int day of week, 0=Sunday * @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 boolean onOrAfter if true and days are same, returns current day * @param string format for returned date * * @access public * * @return string date in given format */
/** * Returns date of the previous specific day of the week * from the given date. * * @param int day of week, 0=Sunday * @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 boolean onOrBefore if true and days are same, returns current day * @param string format for returned date * * @access public * * @return string date in given format */
/** * Returns date of the next specific day of the week * on or before the given date. * * @param int day of week, 0=Sunday * @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 * * @return string date in given format */
function nextDayOfWeekOnOrAfter($dow,$day="",$month="",$year="",$format="%Y%m%d") { return(Date_Calc::nextDayOfWeek($dow,$day="",$month="",$year="",$format="%Y%m%d",true)); } // end func nextDayOfWeekOnOrAfter
/** * Returns date of the previous specific day of the week * on or before the given date. * * @param int day of week, 0=Sunday * @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 * * @return string date in given format */
function prevDayOfWeekOnOrBefore($dow,$day="",$month="",$year="",$format="%Y%m%d") { return(Date_Calc::prevDayOfWeek($dow,$day="",$month="",$year="",$format="%Y%m%d",true));
} // end func prevDayOfWeekOnOrAfter
/** * 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 * * @return string date in given format */
/** * 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 * * @return string date in given format */
/** * 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 * * @return int absolute number of days between dates, * -1 if there is an error. */
function dateDiff($day1,$month1,$year1,$day2,$month2,$year2) { if(!Date_Calc::isValidDate($day1,$month1,$year1)) return -1; if(!Date_Calc::isValidDate($day2,$month2,$year2)) return -1;
/** * Find the number of days in the given month. * * @param string month in format MM, default current local month * * @access public * * @return int number of days */
/** * Returns the number of rows on a calendar month. Useful for * determining the number of rows when displaying a typical * month calendar. * * @param string month in format MM, default current local month * @param string year in format YYCC, default current local year * * @access public * * @return int number of weeks */
/** * 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 * * @return int number of weekday for the first day, 0=Sunday */
/** * 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 * * @return string date in given format */
/** * Find the month day of the beginning of week for given date, * using DATE_CALC_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 * * @return string date in given format */
/** * Find the month day of the end of week for given date, * using DATE_CALC_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 * * @return string date in given format */
/** * Find the month day of the beginning of week after given date, * using DATE_CALC_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 * * @return string date in given format */
/** * Find the month day of the beginning of week before given date, * using DATE_CALC_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 * * @return string date in given format */
/** * 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 * * @return array $week[$weekday] */
/** * 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 * * @return array $month[$row][$col] */
/** * 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 * * @return array $year[$month][$row][$col] */
function getCalendarYear($year="",$format="%Y%m%d") { if(empty($year)) $year = Date_Calc::dateNow("%Y");
/** * 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 * * @return integer number of days */
/** * Converts number of days to a distant unspecified epoch. * * @param int number of days * @param string format for returned date * * @access public * * @return string date in specified format */
/** * 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 dayOfWeek: 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 * * @return string date in given format */
function NWeekdayOfMonth($occurance,$dayOfWeek,$month,$year,$format="%Y%m%d") {
/** * 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 * daysToDate() 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 * * @return string date in given format */
/** * 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 * * @return string abbreviated month name */
function getMonthAbbrname($month,$length=3) { if(empty($month)) $month = Date_Calc::dateNow("%m");
/** * 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 * * @return string full month name */
/** * 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 * * @return string full month name */
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.