/** * 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] */ function getCalendarMonth($month = "", $year = "", $format = "%Y%m%d") { if (empty($year)) { $year = Date_Calc::dateNow("%Y"); } if (empty($month)) { $month = Date_Calc::dateNow("%m"); } $month_array = array(); // date for the first row, first column of calendar month if (DATE_CALC_BEGIN_WEEKDAY == 1) { if (Date_Calc::firstOfMonthWeekday($month, $year) == 0) { $curr_day = Date_Calc::dateToDays("01", $month, $year) - 6; } else { $curr_day = Date_Calc::dateToDays("01", $month, $year) - Date_Calc::firstOfMonthWeekday($month, $year) + 1; } } else { $curr_day = Date_Calc::dateToDays("01", $month, $year) - Date_Calc::firstOfMonthWeekday($month, $year); } // number of days in this month $daysInMonth = Date_Calc::daysInMonth($month, $year); $weeksInMonth = Date_Calc::weeksInMonth($month, $year); for ($row_counter = 0; $row_counter < $weeksInMonth; $row_counter++) { for ($column_counter = 0; $column_counter <= 6; $column_counter++) { $month_array[$row_counter][$column_counter] = Date_Calc::daysToDate($curr_day, $format); $curr_day++; } } return $month_array; }
/** * Subtracts a time span to the date * * Subtracts a time span to the date * * @access public * @param object Date_Span $span the time span to subtract */ function subtractSpan($span) { if (!is_a($span, 'Date_Span')) { return; } if ($span->isEmpty()) { return; } $this->second -= $span->second; if ($this->second < 0) { $this->minute--; $this->second += 60; } $this->minute -= $span->minute; if ($this->minute < 0) { $this->hour--; if ($this->hour < 0) { list($this->year, $this->month, $this->day) = sscanf(Date_Calc::prevDay($this->day, $this->month, $this->year), "%04s%02s%02s"); $this->hour += 24; } $this->minute += 60; } $this->hour -= $span->hour; if ($this->hour < 0) { list($this->year, $this->month, $this->day) = sscanf(Date_Calc::prevDay($this->day, $this->month, $this->year), "%04s%02s%02s"); $this->hour += 24; } $d = Date_Calc::dateToDays($this->day, $this->month, $this->year); $d -= $span->day; list($this->year, $this->month, $this->day) = sscanf(Date_Calc::daysToDate($d), "%04s%02s%02s"); $this->year = intval($this->year); $this->month = intval($this->month); $this->day = intval($this->day); }
/** * Find the month day of the beginning of week after given date, * using DATE_CALC_BEGIN_WEEKDAY * * Can return weekday of prev month. * * @param int $day the day of the month, default is current local day * @param int $month the month, default is current local month * @param int $year the year in four digit format, default is current local year * @param string $format the string indicating how to format the output * * @return string the date in the desired format * * @access public * @static */ function beginOfNextWeek($day = 0, $month = 0, $year = 0, $format = DATE_CALC_FORMAT) { if (empty($year)) { $year = Date_Calc::dateNow('%Y'); } if (empty($month)) { $month = Date_Calc::dateNow('%m'); } if (empty($day)) { $day = Date_Calc::dateNow('%d'); } $date = Date_Calc::daysToDate(Date_Calc::dateToDays($day + 7, $month, $year), '%Y%m%d'); $next_week_year = substr($date, 0, 4); $next_week_month = substr($date, 4, 2); $next_week_day = substr($date, 6, 2); return Date_Calc::beginOfWeek($next_week_day, $next_week_month, $next_week_year, $format); }
/** * Returns date of the last day of the month in the number of months * from the given date * * @param int $months the number of months from the date provided. * Positive numbers go into the future. * Negative numbers go into the past. * 0 is the month presented in $month. * @param string $month the month, default is current local month * @param string $year the year in four digit format, default is the * current local year * @param string $format the string indicating how to format the output * * @return string the date in the desired format * @access public * @static * @since Method available since Release 1.4.4 */ function endOfMonthBySpan($months = 0, $month = 0, $year = null, $format = DATE_CALC_FORMAT) { if (is_null($year)) { $year = Date_Calc::dateNow('%Y'); } if (empty($month)) { $month = Date_Calc::dateNow('%m'); } $hn_days = Date_Calc::addMonthsToDays($months + 1, Date_Calc::firstDayOfMonth($month, $year)) - 1; return Date_Calc::daysToDate($hn_days, $format); }
function &postcalendar_userapi_pcGetEvents($args) { $s_keywords = $s_category = $s_topic = ''; extract($args); $date = postcalendar_getDate(); $cy = substr($date, 0, 4); $cm = substr($date, 4, 2); $cd = substr($date, 6, 2); if (isset($start) && isset($end)) { // parse start date list($sm, $sd, $sy) = explode('/', $start); // parse end date list($em, $ed, $ey) = explode('/', $end); $s = (int) "{$sy}{$sm}{$sd}"; if ($s > $date) { $cy = $sy; $cm = $sm; $cd = $sd; } $start_date = Date_Calc::dateFormat($sd, $sm, $sy, '%Y-%m-%d'); $end_date = Date_Calc::dateFormat($ed, $em, $ey, '%Y-%m-%d'); } else { // missing start OR end date, set them to the current date $sm = $em = $cm; $sd = $ed = $cd; $sy = $cy; $ey = $cy + 2; $start_date = $sy . '-' . $sm . '-' . $sd; $end_date = $ey . '-' . $em . '-' . $ed; } if ($faFlag && !isset($events)) { $a = array('faFlag' => true, 'start' => $start_date, 'end' => $end_date, 's_keywords' => $s_keywords, 's_category' => $s_category, 's_topic' => $s_topic, 'viewtype' => $viewtype, 'provider_id' => $provider_id, 'event_status' => $event_status); $events = pnModAPIFunc(__POSTCALENDAR__, 'user', '<strong></strong>pcQueryEventsFA', $a); } elseif ($collideFlag && !isset($events)) { $a = array('collideFlag' => true, 'start' => $start_date, 'end' => $end_date, 'provider_id' => $provider_id, 'collide_stime' => $stime, 'collide_etime' => $etime); $events = pnModAPIFunc(__POSTCALENDAR__, 'user', 'pcQueryEventsFA', $a); } elseif ($listappsFlag && !isset($events)) { $a = array('listappsFlag' => true, 'start' => $start_date, 'end' => $end_date, 'patient_id' => $patient_id, 's_keywords' => $s_keywords); $events = pnModAPIFunc(__POSTCALENDAR__, 'user', 'pcQueryEvents', $a); } else { if (!isset($events)) { if (!isset($s_keywords)) { $s_keywords = ''; } $a = array('start' => $start_date, 'end' => $end_date, 's_keywords' => $s_keywords, 's_category' => $s_category, 's_topic' => $s_topic, 'viewtype' => $viewtype, "sort" => "pc_startTime ASC, a.pc_duration ASC ", 'providerID' => $providerID, 'provider_id' => $provider_id); $events = pnModAPIFunc(__POSTCALENDAR__, 'user', 'pcQueryEvents', $a); } } //============================================================== // Here we build an array consisting of the date ranges // specific to the current view. This array is then // used to build the calendar display. //============================================================== $days = array(); $sday = Date_Calc::dateToDays($sd, $sm, $sy); $eday = Date_Calc::dateToDays($ed, $em, $ey); for ($cday = $sday; $cday <= $eday; $cday++) { $d = Date_Calc::daysToDate($cday, '%d'); $m = Date_Calc::daysToDate($cday, '%m'); $y = Date_Calc::daysToDate($cday, '%Y'); $store_date = Date_Calc::dateFormat($d, $m, $y, '%Y-%m-%d'); $days[$store_date] = array(); } $days = calculateEvents($days, $events, $viewtype); return $days; }
/** * Converts the date to the specified no of days from the given date * * To subtract days use a negative value for the '$pn_days' parameter * * @param Date $date Date object * @param int $pn_days days to add * * @return Date * @access protected */ function _addDays($date, $pn_days) { $new_date = new Date($date); list($new_date->year, $new_date->month, $new_date->day) = explode(' ', Date_Calc::daysToDate(Date_Calc::dateToDays($date->day, $date->month, $date->year) + $pn_days, '%Y %m %d')); return $new_date; }
/** * Draw the cell label for the day $days * * @param days Date_Calc's 'days' value * @param week week number of this cell in the calendar * @param col column number of this cell in the calendar * * @access public * @return string of the rendered HTML */ function drawCellLabel($days, $week, $col) { $type = $this->getType($days); if (!$this->_otherMonths && $type == 'other_month') { return ' '; } $rv = intval(Date_Calc::daysToDate($days, '%e')); if ($this->_size & HTML_CALENDAR_FULL && ($rv == 1 || $week == 0 && $col == 0) && $this->_otherMonths) { $rv = Date_Calc::daysToDate($days, '%B ') . $rv; } if ($this->_size & HTML_CALENDAR_TINY) { $rv = sprintf('<a href=\'%s\'>%s</a>', $this->makeURL($days), $rv); } return $rv; }
/** * Sets the date/time of the object based on the input date and format * * Accepts a string in three possible formats, and in this order of * precedence: * * - ISO 8601 date (see {@link http://en.wikipedia.org/wiki/ISO_8601}) * - Time-Stamp (i.e. 'YYYYMMDDHHMMSS') * - Unix time-stamp (see {@link http://en.wikipedia.org/wiki/Unix_time}) * * Note that if you want to pass a Unix time-stamp then you need to set * the $format parameter to {@link DATE_FORMAT_UNIXTIME}, or else use the * method {@link Date::setFromTime()}. * * The input string should be a date/time representation in one of the * following general formats: * * - <b><date>T<time><time-zone></b> * - <b><date> <time><time-zone></b> (non-ISO-standard) * - <b><date><time><time-zone></b> (non-ISO-standard) * - <b><date>T<time></b> i.e. without optional <time-zone> representation * - <b><date> <time></b> * - <b><date><time></b> * - <b><date></b> i.e. without optional <time> representation * * that is, the representation must be comprised of a <b><date></b> part, * with an optional <b><time></b> part, which itself may include an optional * <time-zone> part, each of which may consist of any one of the permitted * formats detailed below. The <b><date></b> and <b><time</b> representations * should be divided with the time designator <b>T</b> according to the ISO 8601 * standard, although this method also permits representations divided by a * space, or by no delimiter at all. * * The <b><date></b> representation should be in one of the following formats: * * - <b>Calendar date</b>: <b>YYYY-MM-DD</b> (extended format) or * <b>YYYYMMDD</b> (basic format), where [YYYY] * indicates the four-digit year (0000-9999), [MM] * indicates the month (01-12) and [DD] indicates the * day of the month [01-31] * - <b>ISO week date</b>: <b>YYYY-Www-D</b> (extended format) or * <b>YYYYWwwD</b> (basic format), where [YYYY] * indicates the ISO year (slightly different from the * calendar year (see below)), [Www] indicates the ISO * week no prefixed by the letter 'W' (W01-W53) and * [D] indicates the ISO week-day (1-7), beginning on * Monday and ending on Sunday. (Also see * {@link http://en.wikipedia.org/wiki/ISO_week_date}.) * - <b>Ordinal date</b>: <b>YYYY-DDD</b> (extended format) or * <b>YYYYDDD</b> (basic format), where [YYYY] * indicates the four-digit year (0000-9999) and [DDD] * indicates the day of the year (001-366) * * The <b><time></b> representation should be in one of the following formats: * * - <b>hh:mm:ss</b> (extended format) or <b>hhmmss</b> (basic format) * - <b>hh:mm</b> (extended format) or <b>hhmm</b> (basic format) * - <b>hh</b> (extended format) or <b>hh</b> (basic format) * * where [hh] represents the hour (00-24), [mm] represents the minute (00-59) * and [ss] represents the second (00-60) * * Format parameter should be one of the specified DATE_FORMAT_* constants: * * - <b>{@link DATE_FORMAT_ISO}</b> - 'YYYY-MM-DD HH:MI:SS' * - <b>{@link DATE_FORMAT_ISO_BASIC}</b> - 'YYYYMMDDTHHMMSS(Z|(+/-)HHMM)?' * - <b>{@link DATE_FORMAT_ISO_EXTENDED}</b> - 'YYYY-MM-DDTHH:MM:SS(Z|(+/-)HH:MM)?' * - <b>{@link DATE_FORMAT_ISO_EXTENDED_MICROTIME}</b> - 'YYYY-MM-DDTHH:MM:SS(.S*)?(Z|(+/-)HH:MM)?' * - <b>{@link DATE_FORMAT_TIMESTAMP}</b> - 'YYYYMMDDHHMMSS' * - <b>{@link DATE_FORMAT_UNIXTIME}</b> - long integer of the no of seconds since * the Unix Epoch * (1st January 1970 00.00.00 GMT) * * @param string $date input date * @param int $format optional format constant * (DATE_FORMAT_*) of the input date. * This parameter is not needed, * except to force the setting of the * date from a Unix time-stamp (for * which use * {@link DATE_FORMAT_UNIXTIME}). * (Defaults to * {@link DATE_FORMAT_ISO}.) * @param bool $pb_repeatedhourdefault value to return if repeated * hour is specified (defaults * to false) * * @return void * @access public * @see Date::isNull(), Date::isValidDate(), Date::isValidTime(), * Date::setFromTime() */ function setDate($date, $format = DATE_FORMAT_ISO, $pb_repeatedhourdefault = false) { if ($format == DATE_FORMAT_UNIXTIME) { if (is_numeric($date)) { // Assume Unix time-stamp: // $this->setFromTime((int) $date); } else { return PEAR::raiseError("'{$date}' not valid Unix time-stamp"); } } else { if (preg_match('/^([0-9]{4,4})-?(' . '(0[1-9]|1[0-2])-?(0[1-9]|[12][0-9]|3[01])|' . 'W(0[1-9]|[1-4][0-9]|5[0-3])-?([1-7])|' . '(0(0[1-9]|[1-9][0-9])|[12][0-9]{2,2}|3([0-5][0-9]|6[1-6]))' . ')([T\\s]?' . '([01][0-9]|2[0-3])(:?' . '([0-5][0-9])(:?' . '([0-5][0-9]|60)([,.][0-9]+)?)?)?' . '(Z|[+\\-][0-9]{2,2}(:?[0-5][0-9])?)?)?$/i', $date, $regs)) { if (substr($regs[2], 0, 1) == "W") { // ISO week date (YYYY-Www-D) // $hs_date = Date_Calc::isoWeekToDate($regs[6], $regs[5], $regs[1], "%Y %m %d"); if (PEAR::isError($hs_date)) { return $hs_date; } list($hs_year, $hs_month, $hs_day) = explode(" ", $hs_date); } else { if (strlen($regs[2]) == 3) { // ISO ordinal date (YYYY-DDD) // $hn_jd = Date_Calc::firstDayOfYear($regs[1]) + $regs[2] - 1; list($hs_year, $hs_month, $hs_day) = explode(" ", Date_Calc::daysToDate($hn_jd, "%Y %m %d")); } else { // ISO calendar date (YYYY-MM-DD) // // DATE_FORMAT_ISO, ISO_BASIC, ISO_EXTENDED, and TIMESTAMP // These formats are extremely close to each other. This regex // is very loose and accepts almost any butchered format you could // throw at it. e.g. 2003-10-07 19:45:15 and 2003-10071945:15 // are the same thing in the eyes of this regex, even though the // latter is not a valid ISO 8601 date. // $hs_year = $regs[1]; $hs_month = $regs[3]; $hs_day = $regs[4]; if (!Date_Calc::isValidDate($hs_day, $hs_month, $hs_year)) { return PEAR::raiseError("'" . Date_Calc::dateFormat($hs_year, $hs_month, $hs_day, "%Y-%m-%d") . "' is invalid calendar date", DATE_ERROR_INVALIDDATE); } } } if (isset($regs[17])) { if ($regs[17] == "Z") { $this->tz = new Date_TimeZone("UTC"); } else { $this->tz = new Date_TimeZone("UTC" . $regs[17]); } } $this->setLocalTime($hs_day, $hs_month, $hs_year, isset($regs[11]) && $regs[11] != "" ? $regs[11] : 0, isset($regs[13]) && $regs[13] != "" ? $regs[13] : 0, isset($regs[15]) && $regs[15] != "" ? $regs[15] : 0, isset($regs[16]) && $regs[16] != "" ? $regs[16] : 0.0, $pb_repeatedhourdefault); } else { return PEAR::raiseError("Date '{$date}' not in ISO 8601 format", DATE_ERROR_INVALIDDATEFORMAT); } } }
/** * Subtracts a given number of seconds from the date * * Subtracts a given number of seconds from the date * * @access public * @param int $sec the number of seconds to subtract */ function subtractSeconds($sec) { $days = intval($sec / 86400); $sec -= $days * 86400; $hours = intval($sec / 3600); $sec -= $hours * 3600; $minutes = intval($sec / 60); $sec -= $minutes * 60; $this->second -= $sec; if ($this->second < 0) { $this->minute--; $this->second += 60; } $this->minute -= $minutes; if ($this->minute < 0) { $this->hour--; if ($this->hour < 0) { list($this->year, $this->month, $this->day) = sscanf(Date_Calc::prevDay($this->day, $this->month, $this->year), "%04s%02s%02s"); $this->hour += 24; } $this->minute += 60; } $this->hour -= $hours; if ($this->hour < 0) { list($this->year, $this->month, $this->day) = sscanf(Date_Calc::prevDay($this->day, $this->month, $this->year), "%04s%02s%02s"); $this->hour += 24; } $d = Date_Calc::dateToDays($this->day, $this->month, $this->year); $d -= $days; list($this->year, $this->month, $this->day) = sscanf(Date_Calc::daysToDate($d), "%04s%02s%02s"); }
echo "{$test_name} failed. Expect: {$expect}. Actual: {$actual}\n"; } } } if (php_sapi_name() != 'cli') { echo "<pre>\n"; } compare('20001122', Date_Calc::dateFormat(22, 11, 2000, '%Y%m%d'), 'dateFormat'); compare('20001122', Date_Calc::dateFormat('22', '11', '2000', '%Y%m%d'), 'dateFormat str'); compare('2001', Date_Calc::defaultCentury('1'), 'defaultCentury 1 str'); compare('2001', Date_Calc::defaultCentury(1), 'defaultCentury 1'); compare('1960', Date_Calc::defaultCentury(60), 'defaultCentury 2'); compare('2010', Date_Calc::defaultCentury(10), 'defaultCentury 3'); compare(2451871, Date_Calc::dateToDays('22', '11', '2000'), 'dateToDays str'); compare(2451871, Date_Calc::dateToDays(22, 11, 2000), 'dateToDays'); compare('20001122', Date_Calc::daysToDate(2451871), 'daysToDate'); compare('2000-47-3', Date_Calc::gregorianToISO('22', '11', '2000'), 'gregorianToISO str'); compare('2000-47-3', Date_Calc::gregorianToISO(22, 11, 2000), 'gregorianToISO'); compare(2451716.56767, Date_Calc::dateSeason('SUMMERSOLSTICE', 2000), 'dateSeason'); compare(date('Ymd'), Date_Calc::dateNow(), 'dateNow'); compare(date('Y'), Date_Calc::getYear(), 'getYear'); compare(date('m'), Date_Calc::getMonth(), 'getMonth'); compare(date('d'), Date_Calc::getDay(), 'getDay'); compare(327, Date_Calc::dayOfYear(22, 11, 2000), 'dayOfYear'); compare('November', Date_Calc::getMonthFullname(11), 'getMonthFullname'); compare('Nov', Date_Calc::getMonthAbbrname(11), 'getMonthAbbrname'); compare('Saturday', Date_Calc::getWeekdayFullname(1, 1, 2005), 'getWeekdayFullname'); compare('Sat', Date_Calc::getWeekdayAbbrname(1, 1, 2005), 'getWeekdayAbbrname'); compare(11, Date_Calc::getMonthFromFullName('November'), 'getMonthFromFullName'); compare(327, Date_Calc::dayOfYear('22', '11', '2000'), 'dayOfYear str'); compare('November', Date_Calc::getMonthFullname('11'), 'getMonthFullname str');
/** * Converts the Week number and Day-of-Week to Date * * Calculation algorithm taken from * {@link http://www.merlyn.demon.co.uk/weekcalc.htm}. * * @param int $dow day of week from 1 (Monday) to 7 (Sunday) * @param int $week number of week from 1 to 53 * @param int $year four digits of year * @param string $format the output format * * @return string formatted date * @access public * @static * @since Method available since Release 1.5.0a2 */ function isoWeekToDate($dow, $week, $year, $format = DATE_CALC_FORMAT) { // validates the week number list(, $nweeks) = Date_Calc::isoWeekDate(28, 12, $year); if ($week > $nweeks) { return PEAR::raiseError("ISO week number for {$year} cannot be greater than {$nweeks}", DATE_ERROR_INVALIDDATE); } // validates the day of week if ($dow < 1 || $dow > 7) { return PEAR::raiseError("ISO day of week must be between 1 and 7", DATE_ERROR_INVALIDDATE); } // finds the day of week of January 4th. $jan4th = Date_Calc::dayOfWeek(4, 1, $year); if ($jan4th == 0) { $jan4th = 7; } // offset to the monday of that week $offset = -($jan4th - 1); // increment the days starting from january 4th. $days = Date_Calc::dateToDays(1, 1, $year) + $offset + 7 * ($week - 1) + ($dow - 1) + 3; return Date_Calc::daysToDate($days, $format); }