/**
  *  This function returns the year day of the date.
  *
  *  @param $date   (Optional) A YDDate object, timestamp, array or string.
  *                            If null, the date of the object. Default: null.
  *  @param $format (Optional) The format name. Default: "ISO".
  *
  *  @returns  The year day.
  *
  *  @static   If $date is passed.
  */
 function getYearDay($date = null, $format = "ISO")
 {
     if ($date === null) {
         $date = $this->parse($date, $format);
     } else {
         $date = YDDate::parse($date, $format);
     }
     $yday = 0;
     for ($i = 1; $i < $date['month']; $i++) {
         $yday += YDDate::getDaysInMonth($i, $date['year']);
     }
     return $yday + $date['day'];
 }
 /**
  *  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 += $e->hours - $s->hours;
     $minutes += $e->minutes - $s->minutes;
     $seconds += $e->seconds - $s->seconds;
     $hours += $days * 24;
     $minutes += $hours * 60;
     $seconds += $minutes * 60;
     $count = $days;
     $weekend = $s->getWeekDay() == 0 || $s->getWeekDay() == 6 ? true : false;
     $monthend = $s->day == YDDate::getDaysInMonth($s->month, $s->year) ? true : false;
     $quarterend = $s->month == 3 * $s->getQuarter() && $monthend ? true : false;
     $yearend = $s->month === 12 && $s->day === 31 ? true : false;
     while ($count > 0) {
         if (!($s->getWeekDay() == 0 || $s->getWeekDay() == 6)) {
             if ($weekend) {
                 $weeks++;
             }
             $weekdays++;
             $weekend = false;
         } else {
             $weekend = true;
         }
         if (!($s->day == YDDate::getDaysInMonth($s->month, $s->year))) {
             if ($monthend) {
                 $months++;
             }
             $monthend = false;
         } else {
             $monthend = true;
         }
         if (!($s->month == 3 * $s->getQuarter() && $monthend)) {
             if ($quarterend) {
                 $quarters++;
             }
             $quarterend = false;
         } else {
             $quarterend = true;
         }
         if (!($s->month === 12 && $s->day === 31)) {
             if ($yearend) {
                 $years++;
             }
             $yearend = false;
         } else {
             $yearend = true;
         }
         $s->addDay(1);
         $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;
 }