/** * This function calculates the difference in years, quarters, months, * weeks, weekdays, days, hours, minutes and seconds between the object * and another date passed. * * @param $date The other date to be compared. A YDDate object, * timestamp, array or string. If null, the current date. * * @returns An array with all the difference information. * * @todo Not very efficient for big differences. */ function getDifference($date) { if (!extension_loaded('calendar')) { trigger_error('The PHP calendar extension must be enabled', YD_ERROR); } if ($this->isDateEmpty()) { trigger_error('Cannot make calculations with an empty date', YD_ERROR); } $start = $this; $end = new YDDate($date); $span = array(); $span['years'] = 0; $span['quarters'] = 0; $span['months'] = 0; $span['weeks'] = 0; $span['days'] = 0; $span['weekdays'] = 0; $span['hours'] = 0; $span['minutes'] = 0; $span['seconds'] = 0; if ($start->timestamp_string == $end->timestamp_string) { return $span; } $julian_start = GregoriantoJD($start->month, $start->day, $start->year); $julian_end = GregoriantoJD($end->month, $end->day, $end->year); $days = abs($julian_end - $julian_start); $years = 0; $quarters = 0; $months = 0; $weeks = 0; $weekdays = 0; $hours = 0; $minutes = 0; $seconds = 0; $neg = 1; $s =& $start; $e =& $end; if ($start->timestamp_string > $end->timestamp_string) { $neg = -1; $s =& $end; $e =& $start; } $hours += 24 - $s->hours + $e->hours; if ($s->minutes != $e->minutes) { $minutes += 60 - $s->minutes + $e->minutes; } if ($s->seconds != $s->seconds) { $seconds += 60 - $s->seconds + $e->seconds; } if ($days > 1) { $hours += $days * 24; } $minutes += $hours * 60; $seconds += $minutes * 60; $count = $days; $weekend = $s->isWeekend(); $monthend = $s->isEndOfMonth(); $quarterend = $s->isEndOfQuarter(); $yearend = $s->isEndOfYear(); while ($count > 0) { if (!$s->isWeekend()) { if ($weekend) { $weeks++; } $weekdays++; $weekend = false; } else { $weekend = true; } if (!$s->isEndOfMonth()) { if ($monthend) { $months++; } $monthend = false; } else { $monthend = true; } if (!$s->isEndOfQuarter()) { if ($quarterend) { $quarters++; } $quarterend = false; } else { $quarterend = true; } if (!$s->isEndOfYear()) { if ($yearend) { $years++; } $yearend = false; } else { $yearend = true; } $s->addDay(); $count--; } $span['years'] = $neg * $years; $span['quarters'] = $neg * $quarters; $span['months'] = $neg * $months; $span['weeks'] = $neg * $weeks; $span['days'] = $neg * $days; $span['weekdays'] = $neg * $weekdays; $span['hours'] = $neg * $hours; $span['minutes'] = $neg * $minutes; $span['seconds'] = $neg * $seconds; return $span; }
/** * This function adds a number of days to the date. * * @param $value (Optional) The number of days. Default: 1. * * @returns The date in the 'ISO' format. */ function addDay($value = 1) { if ($value == 0) { return $this->get(); } if (!extension_loaded('calendar')) { trigger_error('The PHP calendar extension must be enabled', YD_ERROR); } if ($this->isDateEmpty()) { trigger_error('Cannot make calculations with an empty date', YD_ERROR); } $julian = GregoriantoJD($this->month, $this->day, $this->year); $julian += $value; $gregorian = JDtoGregorian($julian); list($month, $day, $year) = split('[/]', $gregorian); $this->day = $day; $this->month = $month; $this->year = $year; $this->reset(); return $this->get(); }