/** * Returns number of days between two given dates * * @param int $day1 the day of the month * @param int $month1 the month * @param int $year1 the year. Use the complete year instead of the * abbreviated version. E.g. use 2005, not 05. * @param int $day2 the day of the month * @param int $month2 the month * @param int $year2 the year. Use the complete year instead of the * abbreviated version. E.g. use 2005, not 05. * * @return int the absolute number of days between the two dates. * If an error occurs, -1 is returned. * @access public * @static */ 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; } return abs(Date_Calc::dateToDays($day1, $month1, $year1) - Date_Calc::dateToDays($day2, $month2, $year2)); }
/** * 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 */ function dateFormat($day, $month, $year, $format) { if (!Date_Calc::isValidDate($day, $month, $year)) { $year = Date_Calc::dateNow("%Y"); $month = Date_Calc::dateNow("%m"); $day = Date_Calc::dateNow("%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 .= Date_Calc::getWeekdayAbbrname($day, $month, $year); break; case "A": $output .= Date_Calc::getWeekdayFullname($day, $month, $year); break; case "b": $output .= Date_Calc::getMonthAbbrname($month); break; case "B": $output .= Date_Calc::getMonthFullname($month); break; case "d": $output .= sprintf("%02d", $day); break; case "e": $output .= $day; break; case "E": $output .= Date_Calc::dateToDays($day, $month, $year); break; case "j": $output .= Date_Calc::julianDate($day, $month, $year); break; case "m": $output .= sprintf("%02d", $month); break; case "n": $output .= "\n"; break; case "t": $output .= "\t"; break; case "w": $output .= Date_Calc::dayOfWeek($day, $month, $year); break; case "U": $output .= Date_Calc::weekOfYear($day, $month, $year); 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; }
/** * 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 day in format DD * @param string month in format MM * @param string year in format CCYY * @param string format for returned date * * @access public * * @return string date in given format */ function dateFormat($day, $month, $year, $format) { if (!Date_Calc::isValidDate($day, $month, $year)) { $year = Date_Calc::dateNow('%Y'); $month = Date_Calc::dateNow('%m'); $day = Date_Calc::dateNow('%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 .= Date_Calc::getWeekdayAbbrname($day, $month, $year); break; case 'A': $output .= Date_Calc::getWeekdayFullname($day, $month, $year); break; case 'b': $output .= Date_Calc::getMonthAbbrname($month); break; case 'B': $output .= Date_Calc::getMonthFullname($month); break; case 'd': $output .= sprintf('%02d', $day); break; case 'e': $output .= $day; break; case 'E': $output .= Date_Calc::dateToDays($day, $month, $year); break; case 'j': $output .= Date_Calc::julianDate($day, $month, $year); break; case 'm': $output .= sprintf('%02d', $month); break; case 'n': $output .= "\n"; break; case "t": $output .= "\t"; break; case 'w': $output .= Date_Calc::dayOfWeek($day, $month, $year); break; case 'U': $output .= Date_Calc::weekOfYear($day, $month, $year); 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; }
/** * Sets all the fields of the date object (day, month, year, hour, minute * and second) * * If specified year forms an invalid date, then PEAR error will be * returned. Note that setting each of these fields separately * may unintentionally return a PEAR error if a transitory date is * invalid between setting these fields. * * N.B. if the repeated hour, due to the clocks going back, is specified, * the default is to assume local standard time. * * @param int $pn_day the day * @param int $pn_month the month * @param int $pn_year the year * @param int $pn_hour the hour * @param int $pn_minute the minute * @param mixed $pm_second the second as integer or float * @param bool $pb_repeatedhourdefault whether to assume Summer time if a * repeated hour is specified * (defaults to false) * * @return void * @access public * @see Date::setDayMonthYear(), Date::setHourMinuteSecond() * @since Method available since Release 1.5.0 */ function setDateTime($pn_day, $pn_month, $pn_year, $pn_hour, $pn_minute, $pm_second, $pb_repeatedhourdefault = false) { if (!Date_Calc::isValidDate($d, $m, $y)) { return PEAR::raiseError("'" . Date_Calc::dateFormat($d, $m, $y, "%Y-%m-%d") . "' is invalid calendar date", DATE_ERROR_INVALIDDATE); } else { // Split second into integer and part-second: // if (is_float($pm_second)) { $hn_second = intval($pm_second); $hn_partsecond = $pm_second - $hn_second; } else { $hn_second = (int) $pm_second; $hn_partsecond = 0.0; } $this->setLocalTime($d, $m, $y, $h, $m, $hn_second, $hn_partsecond, $pb_repeatedhourdefault); } }
/** * Creates the rec_data array. * * @param array $A Array of data, default to $_POST */ public function MakeRecData($A = '') { if ($A == '') { $A = $_POST; } // Re-initialize the array, and make sure this is really a $this->rec_data = array(); if (!isset($A['recurring']) || $A['recurring'] != 1) { $this->rec_data['type'] = 0; $this->rec_data['stop'] = EV_MAX_DATE; $this->rec_data['freq'] = 1; return; } else { $this->rec_data['type'] = isset($A['format']) ? (int) $A['format'] : 0; $this->rec_data['freq'] = isset($A['rec_freq']) ? (int) $A['rec_freq'] : 1; if ($this->rec_data['freq'] < 1) { $this->rec_data['freq'] = 1; } } if (!empty($A['stopdate'])) { list($stop_y, $stop_m, $stop_d) = explode('-', $A['stopdate']); if (Date_Calc::isValidDate($stop_d, $stop_m, $stop_y)) { $this->rec_data['stop'] = $A['stopdate']; } } switch ($this->rec_data['type']) { case EV_RECUR_WEEKLY: if (isset($A['listdays']) && is_array($A['listdays'])) { $this->rec_data['listdays'] = array(); foreach ($A['listdays'] as $day) { $this->rec_data['listdays'][] = (int) $day; } } break; case EV_RECUR_MONTHLY: if (isset($A['mdays']) && is_array($A['mdays'])) { $this->rec_data['listdays'] = array(); foreach ($A['mdays'] as $mday) { $this->rec_data['listdays'][] = (int) $mday; } } // ... fall through to handle weekend skipping // ... fall through to handle weekend skipping case EV_RECUR_DAILY: case EV_RECUR_YEARLY: // Set weekend skip- applies to Monthly, Daily and Yearly $this->rec_data['skip'] = isset($A['skipnext']) ? (int) $A['skipnext'] : 0; break; case EV_RECUR_DOM: $this->rec_data['weekday'] = (int) $A['weekday']; $this->rec_data['interval'] = is_array($A['interval']) ? $A['interval'] : array($A['interval']); break; case EV_RECUR_DATES: // Specific dates. Simple handling. $recDates = preg_split('/[\\s,]+/', $A['custom']); sort($recDates); // why not keep them in order... $this->rec_data['custom'] = $recDates; /*foreach($recDates as $occurrence) { list($y, $m, $d) = explode('-', $occurrence); if (Date_Calc::isValidDate($d, $m, $y)) { $events[] = array( 'dt_start' => $occurrence, 'dt_end' => $occurrence, 'tm_start1' => $this->time_start1, 'tm_end1' => $this->time_end1, 'tm_start2' => $this->time_start2, 'tm_end2' => $this->time_end2, ); } } // We have the dates, don't need to go through the loop. return $events;*/ break; default: // Unknown value, nothing to do break; } }
/** Retorna true si el objeto $date (clase Date) pasado es válido, * caso contrario retorna false. */ public function isValidDate($date) { if (!Date_Calc::isValidDate($date->getDay(), $date->getMonth(), $date->getYear())) { return false; } return true; }
public function MakeRecurrences() { if (!is_array($this->event->rec_data['custom'])) { return $this->events; } foreach ($this->event->rec_data['custom'] as $occurrence) { list($y, $m, $d) = explode('-', $occurrence); if (Date_Calc::isValidDate($d, $m, $y)) { $this->storeEvent($occurrence); } } return $this->events; }
/** * 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 */ public static function dateFormat($day, $month, $year, $format) { if (!Date_Calc::isValidDate($day, $month, $year)) { $year = Date_Calc::dateNow("%Y"); $month = Date_Calc::dateNow("%m"); $day = Date_Calc::dateNow("%d"); } $output = ""; $myTime = mktime(0, 0, 0, $month, $day, $year); 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 .= date('D', $myTime); break; case "A": $output .= date('l', $myTime); break; case "b": setlocale(LC_TIME, 'en'); $output .= date('M', $myTime); setlocale(LC_ALL, $AppUI->user_lang); break; case "B": $output .= date('F', $myTime); break; case "d": $output .= sprintf("%02d", $day); break; case "e": $output .= $day; break; case "E": $output .= Date_Calc::dateToDays($day, $month, $year); break; case "j": $output .= date('z', $myTime); break; case "m": $output .= sprintf("%02d", $month); break; case "n": $output .= "\n"; break; case "t": $output .= "\t"; break; case "w": $output .= date('w', $myTime); break; case "U": $output .= date('W', $myTime); break; case "y": $output .= date('y', $myTime); break; case "Y": $output .= $year; break; case "%": $output .= "%"; break; default: $output .= $char . $nextchar; } $strpos++; } else { $output .= $char; } } return $output; }
compare('20050130', Date_Calc::NWeekdayOfMonth('last', 0, 1, 2005), 'NWeekdayOfMonth l01'); compare('20050129', Date_Calc::NWeekdayOfMonth('last', 6, 1, 2005), 'NWeekdayOfMonth l61'); compare('20050128', Date_Calc::NWeekdayOfMonth('last', 5, 1, 2005), 'NWeekdayOfMonth l51'); compare('20050127', Date_Calc::NWeekdayOfMonth('last', 4, 1, 2005), 'NWeekdayOfMonth l41'); compare('20050126', Date_Calc::NWeekdayOfMonth('last', 3, 1, 2005), 'NWeekdayOfMonth l31'); compare('20050125', Date_Calc::NWeekdayOfMonth('last', 2, 1, 2005), 'NWeekdayOfMonth l21'); compare('20050331', Date_Calc::NWeekdayOfMonth('last', 4, 3, 2005), 'NWeekdayOfMonth l43'); compare('20050330', Date_Calc::NWeekdayOfMonth('last', 3, 3, 2005), 'NWeekdayOfMonth l33'); compare('20050329', Date_Calc::NWeekdayOfMonth('last', 2, 3, 2005), 'NWeekdayOfMonth l23'); compare('20050328', Date_Calc::NWeekdayOfMonth('last', 1, 3, 2005), 'NWeekdayOfMonth l13'); compare('20050327', Date_Calc::NWeekdayOfMonth('last', 0, 3, 2005), 'NWeekdayOfMonth l03'); compare('20050326', Date_Calc::NWeekdayOfMonth('last', 6, 3, 2005), 'NWeekdayOfMonth l63'); compare('20050325', Date_Calc::NWeekdayOfMonth('last', 5, 3, 2005), 'NWeekdayOfMonth l53'); compare(false, Date_Calc::isValidDate(29, 2, 1900), 'isValidDate 1'); compare(true, Date_Calc::isValidDate(29, 2, 2000), 'isValidDate 2'); compare(true, Date_Calc::isValidDate('29', '02', '2000'), 'isValidDate 2 str'); compare(false, Date_Calc::isLeapYear(1900), 'isLeapYear 1'); compare(true, Date_Calc::isLeapYear(1996), 'isLeapYear 2'); compare(true, Date_Calc::isLeapYear(2000), 'isLeapYear 3'); compare(false, Date_Calc::isLeapYear(2001), 'isLeapYear 4'); compare(false, Date_Calc::isLeapYear('2001'), 'isLeapYear 4 str'); compare(false, Date_Calc::isFutureDate('22', '11', '2000'), 'isFutureDate 1 str'); compare(false, Date_Calc::isFutureDate(22, 11, 2000), 'isFutureDate 1'); compare(true, Date_Calc::isFutureDate(22, 11, date('Y') + 1), 'isFutureDate 2'); compare(false, Date_Calc::isPastDate(22, 11, date('Y') + 1), 'isPastDate 1'); compare(true, Date_Calc::isPastDate(22, 11, 2000), 'isPastDate 2'); compare(true, Date_Calc::isPastDate('22', '11', '2000'), 'isPastDate 2 str'); compare(10, Date_Calc::dateDiff(22, 11, 2000, 12, 11, 2000), 'dateDiff 1'); compare(10, Date_Calc::dateDiff(12, 11, 2000, 22, 11, 2000), 'dateDiff 2'); compare(61, Date_Calc::dateDiff(22, 11, 2000, 22, 1, 2001), 'dateDiff 3'); compare(61, Date_Calc::dateDiff('22', '11', '2000', '22', '01', '2001'), 'dateDiff 3 str');