--- /usr/local/apache/htdocs/mail.kwenie.org/pbcs-0.6.2/util_funcs.php	2004-06-17 21:57:02.000000000 +0200
+++ util_funcs.php	2004-12-15 21:53:30.000000000 +0100
@@ -173,18 +173,38 @@
 }
 
 function get_week_nr( $date ) {
-	$date_year = date("Y",$date);
+// I never knew that calculating weeknumber can be so tricky... To get it all right
+// I read some official docs about ISO time and date stuff. Here is a description about 
+// what the first week will be.
+//
+// In commercial and industrial applications (delivery times, production plans, etc.), 
+// especially in Europe, it is often required to refer to a week of a year. Week 01 of
+// a year is per definition the first week that has the Thursday in this year, which 
+// is equivalent to the week that contains the fourth day of January. In other words, 
+// the first week of a new year is the week that has the majority of its days in the 
+// new year. Week 01 might also contain days from the previous year and the week before 
+// week 01 of a year is the last week (52 or 53) of the previous year even if it contains
+// days from the new year. A week starts with Monday (day 1) and ends with Sunday (day 7).  
+//
+// I finally took some code from the Pear Date library. That is what Open Source is good 
+// for.
+	$day   = date("j",$date);
+	$month = date("n",$date);
+	$year  = date("Y",$date);
+	return weekOfYear( $day, $month , $year );
+
+
+
+/*	$date_year = date("Y",$date);
 
 	$first_year_week_day = date("w",mktime( 0,0,0,1,1,$date_year ) );
 	if( $first_year_week_day == 0 ) $first_year_week_day = 7;
 	$first_year_week_day -= 1;
-#       echo "1 jan $date_year is on day $first_year_week_day of week. ".date("l",mktime( 0,0,0,1,1,$date_year ))." <br>";
 
-#       echo date("z", $date );
 
 	return intval( ($first_year_week_day + date("z", $date )) / 7) + 1;
 
-#	return (intval(intval(date("z",	$date )+1)/7) + 1);
+*/
 }
 
 function print_duration( $start_time , $end_time ) {
@@ -194,6 +214,161 @@
 	return $hours."H ".$rest."m";
 }
 
+// START: I lend some code from the PEAR Date 1.4.3 object, but I don't need all they have...
+    /**
+     * Returns the current local date. NOTE: This function
+     * retrieves the local date using strftime(), which may
+     * or may not be 32-bit safe on your system.
+     *
+     * @param string the strftime() format to return the date
+     *
+     * @access public
+     *
+     * @return string the current date in specified format
+     */
+
+    function dateNow($format='%Y%m%d')
+    {
+        return(strftime($format,time()));
+
+    } // end func dateNow
+
+
+     /**
+     * Returns true for a leap year, else false
+     *
+     * @param string year in format CCYY
+     *
+     * @access public
+     *
+     * @return boolean true/false
+     */
+
+    function isLeapYear($year='')
+    {
+        if (empty($year)) {
+            $year = dateNow('%Y');
+        }
+
+        if (preg_match('/\D/',$year)) {
+            return false;
+        }
+
+        if ($year < 1000) {
+            return false;
+        }
+
+        if ($year < 1582) {
+            // pre Gregorio XIII - 1582
+            return ($year % 4 == 0);
+        } else {
+            // post Gregorio XIII - 1582
+            return ( (($year % 4 == 0) and ($year % 100 != 0)) or ($year % 400 == 0) );
+        }
+    } // end func isLeapYear
+
+
+    /**
+     * Converts from Gregorian Year-Month-Day to
+     * ISO YearNumber-WeekNumber-WeekDay
+     *
+     * Uses ISO 8601 definitions.
+     * Algorithm from Rick McCarty, 1999 at
+     * http://personal.ecu.edu/mccartyr/ISOwdALG.txt
+     *
+     * @param string day in format DD
+     * @param string month in format MM
+     * @param string year in format CCYY
+     * @return string
+     * @access public
+     */
+    // Transcribed to PHP by Jesus M. Castagnetto (blame him if it is fubared ;-)
+    function gregorianToISO($day,$month,$year) {
+        $mnth = array (0,31,59,90,120,151,181,212,243,273,304,334);
+        $y_isleap = isLeapYear($year);
+        $y_1_isleap = isLeapYear($year - 1);
+        $day_of_year_number = $day + $mnth[$month - 1];
+        if ($y_isleap && $month > 2) {
+            $day_of_year_number++;
+        }
+        // find Jan 1 weekday (monday = 1, sunday = 7)
+        $yy = ($year - 1) % 100;
+        $c = ($year - 1) - $yy;
+        $g = $yy + intval($yy/4);
+        $jan1_weekday = 1 + intval((((($c / 100) % 4) * 5) + $g) % 7);
+        // weekday for year-month-day
+        $h = $day_of_year_number + ($jan1_weekday - 1);
+        $weekday = 1 + intval(($h - 1) % 7);
+        // find if Y M D falls in YearNumber Y-1, WeekNumber 52 or
+        if ($day_of_year_number <= (8 - $jan1_weekday) && $jan1_weekday > 4){
+            $yearnumber = $year - 1;
+            if ($jan1_weekday == 5 || ($jan1_weekday == 6 && $y_1_isleap)) {
+                $weeknumber = 53;
+            } else {
+                $weeknumber = 52;
+            }
+        } else {
+            $yearnumber = $year;
+        }
+        // find if Y M D falls in YearNumber Y+1, WeekNumber 1
+        if ($yearnumber == $year) {
+            if ($y_isleap) {
+                $i = 366;
+            } else {
+                $i = 365;
+            }
+            if (($i - $day_of_year_number) < (4 - $weekday)) {
+                $yearnumber++;
+                $weeknumber = 1;
+            }
+        }
+        // find if Y M D falls in YearNumber Y, WeekNumber 1 through 53
+        if ($yearnumber == $year) {
+            $j = $day_of_year_number + (7 - $weekday) + ($jan1_weekday - 1);
+            $weeknumber = intval($j / 7);
+            if ($jan1_weekday > 4) {
+                $weeknumber--;
+            }
+        }
+        // put it all together
+        if ($weeknumber < 10)
+            $weeknumber = '0'.$weeknumber;
+        return "{$yearnumber}-{$weeknumber}-{$weekday}";
+    }
+
+    /**
+     * Returns week of the year, first Sunday is first day of first week
+     *
+     * @param string day in format DD, default is current local day
+     * @param string month in format MM, default is current local month
+     * @param string year in format CCYY, default is current local year
+     *
+     * @access public
+     *
+     * @return integer $week_number
+     */
+
+    function weekOfYear($day='',$month='',$year='')
+    {
+        if (empty($year)) {
+            $year = dateNow('%Y');
+        }
+        if (empty($month)) {
+            $month = dateNow('%m');
+        }
+        if (empty($day)) {
+            $day = dateNow('%d');
+        }
+        $iso    = gregorianToISO($day, $month, $year);
+        $parts  = explode('-',$iso);
+        $week_number = intval($parts[1]);
+        return $week_number;
+    } // end func weekOfYear
+
+
+
+// END: I lend some code from the PEAR Date object, but I don't need all they have...
+
 
 //********** APPOINTMENT FUNCTIONS
 
