/** * Calculates the number of days between first and second parameter * * @param int Date 1 * @param int Date 2 * @return int Number of days * * @author Former03 GmbH :: Florian Lippert <*****@*****.**> */ function calculateDayDifference($begin, $end) { $daycount = 0; $begin = transferDateToArray($begin); $end = transferDateToArray($end); $direction = 1; // Sanity check, if our given array is in the right format if (checkDateArray($begin) === true && checkDateArray($end) === true) { if (strtotime($end['y'] . '-' . $end['m'] . '-' . $end['d']) < strtotime($begin['y'] . '-' . $begin['m'] . '-' . $begin['d'])) { $tmp = $end; $end = $begin; $begin = $tmp; unset($tmp); $direction = -1; } $yeardiff = (int) $end['y'] - (int) $begin['y']; $monthdiff = (int) $end['m'] + 12 * $yeardiff - (int) $begin['m']; for ($i = 0; $i < abs($monthdiff); $i++) { $daycount += getDaysForMonth($begin['m'] + $i, $begin['y']); } $daycount += $end['d'] - $begin['d']; $daycount *= $direction; } return $daycount; }
/** * Manipulates a date, like adding a month or so and correcting it afterwards * (2008-01-33 -> 2008-02-02) * * @param array The date array * @param string The operation, may be '+', 'add', 'sum' or '-', 'subtract', 'subduct' * @param int Number if days/month/years * @param string Either 'y', 'm', 'd', depending on what part to change. * @param array A valid date array with original date, mandatory for more than one manipulation on same date. * @return date The manipulated date array * * @author Former03 GmbH :: Florian Lippert <*****@*****.**> */ function manipulateDate($date, $operation, $count, $type, $original_date = null) { $newdate = $date; $date = transferDateToArray($date); if (checkDateArray($date) === true && isset($date[$type])) { switch ($operation) { case '+': case 'add': case 'sum': $date[$type] += (int) $count; break; case '-': case 'subtract': case 'subduct': $date[$type] -= (int) $count; break; } if ($original_date !== null && ($original_date = transferDateToArray($original_date)) !== false && $type == 'm') { if ($original_date['d'] > getDaysForMonth($date['m'], $date['y'])) { $date['d'] = getDaysForMonth($date['m'], $date['y']) - (getDaysForMonth($original_date['m'], $original_date['y']) - $original_date['d']); } else { $date['d'] = $original_date['d']; } } while (checkDateArray($date) === false) { if ($date['d'] > getDaysForMonth($date['m'], $date['y'])) { $date['d'] -= getDaysForMonth($date['m'], $date['y']); $date['m']++; } if ($date['d'] < 1) { $date['m']--; $date['d'] += getDaysForMonth($date['m'], $date['y']); // Adding here, because date[d] is negative } if ($date['m'] > 12) { $date['m'] -= 12; $date['y']++; } if ($date['m'] < 1) { $date['y']--; $date['m'] += 12; } } $newdate = $date['y'] . '-' . $date['m'] . '-' . $date['d']; } return $newdate; }
/** * Due to the weirdness in our calendar we need a special * method for gathering the number days in an interval. * * @param int Length of interval * @param string Type of interval, might be 'd' for day,'m' for month or 'y' for year * @param array Date when service began, in array form: array( 'd' => int, 'm' => int, 'y' => int ) * * @return int Number of days in the interval * * @author Former03 GmbH :: Florian Lippert <*****@*****.**> */ public function getDaysForInterval($interval_length, $interval_type, $service_date_begin_array) { $returnval = 0; switch ($interval_type) { case 'y': for ($i = 1; $i <= $interval_length; ++$i) { $returnval += getDaysForYear((int) $service_date_begin_array['m'], (int) $service_date_begin_array['y'] + ($i - 1)); } break; case 'm': for ($i = 1; $i <= $interval_length; ++$i) { $returnval += getDaysForMonth((int) $service_date_begin_array['m'] + ($i - 1), (int) $service_date_begin_array['y']); } break; case 'd': default: $returnval = $interval_length; break; } return $returnval; }
/** * Checks if a date array is valid. * * @param array The date array * * @return bool True if valid, false otherwise. * * @author Former03 GmbH :: Florian Lippert <*****@*****.**> */ function checkDateArray($date) { return is_array($date) && isset($date['y']) && isset($date['m']) && isset($date['d']) && (int) $date['m'] >= 1 && (int) $date['m'] <= 12 && (int) $date['d'] >= 1 && (int) $date['d'] <= getDaysForMonth($date['m'], $date['y']); }