Beispiel #1
0
function isbatchingmaintenancecurrent($batching_expiration_date)
{
    $bedate = $batching_expiration_date ? new CDate($batching_expiration_date) : null;
    $now = new CDate();
    if (Date_Calc::compareDates($bedate->day, $bedate->month, $bedate->year, $now->day, $now->month, $now->year) < 0) {
        return "No";
    } else {
        return "Yes";
    }
}
Beispiel #2
0
 public static function getAgo($timestamp)
 {
     if ($timestamp === null) {
         return '';
     }
     $diffdays = Date_Calc::dateDiff(date('j', $timestamp), date('n', $timestamp), date('Y', $timestamp), date('j'), date('n'), date('Y'));
     /* An error occured. */
     if ($diffdays == -1) {
         return;
     }
     $ago = $diffdays * Date_Calc::compareDates(date('j', $timestamp), date('n', $timestamp), date('Y', $timestamp), date('j'), date('n'), date('Y'));
     if ($ago < -1) {
         return sprintf(Horde_Model_Translation::t(" (%s days ago)"), $diffdays);
     } elseif ($ago == -1) {
         return Horde_Model_Translation::t(" (yesterday)");
     } elseif ($ago == 0) {
         return Horde_Model_Translation::t(" (today)");
     } elseif ($ago == 1) {
         return Horde_Model_Translation::t(" (tomorrow)");
     } else {
         return sprintf(Horde_Model_Translation::t(" (in %s days)"), $diffdays);
     }
 }
Beispiel #3
0
 /**
  * Validate date and times. Note that this method need the Date_Calc class
  *
  * @param string    $date   Date to validate
  * @param array     $options array options where :
  *                          'format' The format of the date (%d-%m-%Y)
  *                                   or rfc822_compliant
  *                          'min' The date has to be greater
  *                                than this array($day, $month, $year)
  *                                or PEAR::Date object
  *                          'max' The date has to be smaller than
  *                                this array($day, $month, $year)
  *                                or PEAR::Date object
  *
  * @return boolean true if valid date/time, false if not
  *
  * @access public
  */
 function date($date, $options)
 {
     $max = $min = false;
     $format = '';
     if (is_array($options)) {
         extract($options);
     }
     if (strtolower($format) == 'rfc822_compliant') {
         $preg = '&^(?:(Mon|Tue|Wed|Thu|Fri|Sat|Sun),) \\s+
                 (?:(\\d{2})?) \\s+
                 (?:(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)?) \\s+
                 (?:(\\d{2}(\\d{2})?)?) \\s+
                 (?:(\\d{2}?)):(?:(\\d{2}?))(:(?:(\\d{2}?)))? \\s+
                 (?:[+-]\\d{4}|UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Za-ik-z])$&xi';
         if (!preg_match($preg, $date, $matches)) {
             return false;
         }
         $year = (int) $matches[4];
         $months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
         $month = array_keys($months, $matches[3]);
         $month = (int) $month[0] + 1;
         $day = (int) $matches[2];
         $weekday = $matches[1];
         $hour = (int) $matches[6];
         $minute = (int) $matches[7];
         isset($matches[9]) ? $second = (int) $matches[9] : ($second = 0);
         if (strlen($year) != 4 || ($day > 31 || $day < 1) || $hour > 23 || $minute > 59 || $second > 59) {
             return false;
         }
     } else {
         $date_len = strlen($format);
         for ($i = 0; $i < $date_len; $i++) {
             $c = $format[$i];
             if ($c == '%') {
                 $next = $format[$i + 1];
                 switch ($next) {
                     case 'j':
                     case 'd':
                         if ($next == 'j') {
                             $day = (int) Validate::_substr($date, 1, 2);
                         } else {
                             $day = (int) Validate::_substr($date, 0, 2);
                         }
                         if ($day < 1 || $day > 31) {
                             return false;
                         }
                         break;
                     case 'm':
                     case 'n':
                         if ($next == 'm') {
                             $month = (int) Validate::_substr($date, 0, 2);
                         } else {
                             $month = (int) Validate::_substr($date, 1, 2);
                         }
                         if ($month < 1 || $month > 12) {
                             return false;
                         }
                         break;
                     case 'Y':
                     case 'y':
                         if ($next == 'Y') {
                             $year = Validate::_substr($date, 4);
                             $year = (int) $year ? $year : '';
                         } else {
                             $year = (int) (substr(date('Y'), 0, 2) . Validate::_substr($date, 2));
                         }
                         if (strlen($year) != 4 || $year < 0 || $year > 9999) {
                             return false;
                         }
                         break;
                     case 'g':
                     case 'h':
                         if ($next == 'g') {
                             $hour = Validate::_substr($date, 1, 2);
                         } else {
                             $hour = Validate::_substr($date, 2);
                         }
                         if (!preg_match('/^\\d+$/', $hour) || $hour < 0 || $hour > 12) {
                             return false;
                         }
                         break;
                     case 'G':
                     case 'H':
                         if ($next == 'G') {
                             $hour = Validate::_substr($date, 1, 2);
                         } else {
                             $hour = Validate::_substr($date, 2);
                         }
                         if (!preg_match('/^\\d+$/', $hour) || $hour < 0 || $hour > 24) {
                             return false;
                         }
                         break;
                     case 's':
                     case 'i':
                         $t = Validate::_substr($date, 2);
                         if (!preg_match('/^\\d+$/', $t) || $t < 0 || $t > 59) {
                             return false;
                         }
                         break;
                     default:
                         trigger_error("Not supported char `{$next}' after % in offset " . ($i + 2), E_USER_WARNING);
                 }
                 $i++;
             } else {
                 //literal
                 if (Validate::_substr($date, 1) != $c) {
                     return false;
                 }
             }
         }
     }
     // there is remaing data, we don't want it
     if (strlen($date) && strtolower($format) != 'rfc822_compliant') {
         return false;
     }
     if (isset($day) && isset($month) && isset($year)) {
         if (!checkdate($month, $day, $year)) {
             return false;
         }
         if (strtolower($format) == 'rfc822_compliant') {
             if ($weekday != date("D", mktime(0, 0, 0, $month, $day, $year))) {
                 return false;
             }
         }
         if ($min) {
             include_once 'Date/Calc.php';
             if (is_a($min, 'Date') && Date_Calc::compareDates($day, $month, $year, $min->getDay(), $min->getMonth(), $min->getYear()) < 0) {
                 return false;
             } elseif (is_array($min) && Date_Calc::compareDates($day, $month, $year, $min[0], $min[1], $min[2]) < 0) {
                 return false;
             }
         }
         if ($max) {
             include_once 'Date/Calc.php';
             if (is_a($max, 'Date') && Date_Calc::compareDates($day, $month, $year, $max->getDay(), $max->getMonth(), $max->getYear()) > 0) {
                 return false;
             } elseif (is_array($max) && Date_Calc::compareDates($day, $month, $year, $max[0], $max[1], $max[2]) > 0) {
                 return false;
             }
         }
     }
     return true;
 }
Beispiel #4
0
 /**
  * Validate date and times. Note that this method need the Date_Calc class
  *
  * @param string    $date   Date to validate
  * @param array     $options array options where :
  *                          'format' The format of the date (%d-%m-%Y)
  *                          'min' The date has to be greater
  *                                than this array($day, $month, $year)
  *                          'max' The date has to be smaller than
  *                                this array($day, $month, $year)
  *
  * @return bool
  */
 function date($date, $options)
 {
     $max = $min = false;
     $format = '';
     if (is_array($options)) {
         extract($options);
     }
     $date_len = strlen($format);
     for ($i = 0; $i < strlen($format); $i++) {
         $c = $format[$i];
         if ($c == '%') {
             $next = $format[$i + 1];
             switch ($next) {
                 case 'j':
                 case 'd':
                     if ($next == 'j') {
                         $day = (int) Validate::_substr($date, 1, 2);
                     } else {
                         $day = (int) Validate::_substr($date, 2);
                     }
                     if ($day < 1 || $day > 31) {
                         return false;
                     }
                     break;
                 case 'm':
                 case 'n':
                     if ($next == 'm') {
                         $month = (int) Validate::_substr($date, 2);
                     } else {
                         $month = (int) Validate::_substr($date, 1, 2);
                     }
                     if ($month < 1 || $month > 12) {
                         return false;
                     }
                     break;
                 case 'Y':
                 case 'y':
                     if ($next == 'Y') {
                         $year = Validate::_substr($date, 4);
                         $year = (int) $year ? $year : '';
                     } else {
                         $year = (int) (substr(date('Y'), 0, 2) . Validate::_substr($date, 2));
                     }
                     if (strlen($year) != 4 || $year < 0 || $year > 9999) {
                         return false;
                     }
                     break;
                 case 'g':
                 case 'h':
                     if ($next == 'g') {
                         $hour = Validate::_substr($date, 1, 2);
                     } else {
                         $hour = Validate::_substr($date, 2);
                     }
                     if ($hour < 0 || $hour > 12) {
                         return false;
                     }
                     break;
                 case 'G':
                 case 'H':
                     if ($next == 'G') {
                         $hour = Validate::_substr($date, 1, 2);
                     } else {
                         $hour = Validate::_substr($date, 2);
                     }
                     if ($hour < 0 || $hour > 24) {
                         return false;
                     }
                     break;
                 case 's':
                 case 'i':
                     $t = Validate::_substr($date, 2);
                     if ($t < 0 || $t > 59) {
                         return false;
                     }
                     break;
                 default:
                     trigger_error("Not supported char `{$next}' after % in offset " . ($i + 2), E_USER_WARNING);
             }
             $i++;
         } else {
             //literal
             if (Validate::_substr($date, 1) != $c) {
                 return false;
             }
         }
     }
     // there is remaing data, we don't want it
     if (strlen($date)) {
         return false;
     }
     if (isset($day) && isset($month) && isset($year)) {
         if (!checkdate($month, $day, $year)) {
             return false;
         }
         if ($min || $max) {
             include_once 'Date/Calc.php';
             if ($min && Date_Calc::compareDates($day, $month, $year, $min[0], $min[1], $min[2]) < 0) {
                 return false;
             }
             if ($max && Date_Calc::compareDates($day, $month, $year, $max[0], $max[1], $max[2]) > 0) {
                 return false;
             }
         }
     }
     return true;
 }
Beispiel #5
0
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');
compare(-1, Date_Calc::compareDates(12, 11, 2000, 22, 11, 2000), 'compareDates 1');
compare(0, Date_Calc::compareDates(22, 11, 2000, 22, 11, 2000), 'compareDates 2');
compare(1, Date_Calc::compareDates(22, 11, 2000, 12, 11, 2000), 'compareDates 3');
compare(1, Date_Calc::compareDates('22', '11', '2000', '12', '11', '2000'), 'compareDates 3 str');