#19719 [Opn->Fbk]: wrong week number of year at year end / beginn

From: Date: Wed, 02 Oct 2002 15:45:29 +0000
Subject: #19719 [Opn->Fbk]: wrong week number of year at year end / beginn
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-21292@lists.php.net to get a copy of this message
ID: 19719 Updated by: rasmus@php.net Reported By: martin.adler@continum.net -Status: Open +Status: Feedback Bug Type: Calendar related Operating System: FreeBSD 4.3-RELEASE PHP Version: 4.2.2 New Comment: Please provide a *short* script that illustrates this problem. Sounds like you could have illustrated this with a 1-line script. Previous Comments: ------------------------------------------------------------------------ [2002-10-02 10:43:53] martin.adler@continum.net The date-function doesn't return the korrect week number of the year. The 31'rd December 2001 is a Monday but belongs to the first Week in 2002. The Rule is: The first January belongs to the first week of year, if this day is a Monday,Tuesday,Wednesday or Thursday. ################## Script ########################### <? $cal = new calendar(mktime(0,0,0,1,1,2002)); ?> <table cellspacing="1" cellpadding="5" border="0"> <tr> <td bgcolor="#e0e0e0"><b>KW</b></td> <td bgcolor="#e0e0e0"><b>Mo</b></td> <td bgcolor="#e0e0e0"><b>Di</b></td> <td bgcolor="#e0e0e0"><b>Mi</b></td> <td bgcolor="#e0e0e0"><b>Do</b></td> <td bgcolor="#e0e0e0"><b>Fr</b></td> <td bgcolor="#e0e0e0"><b>Sa</b></td> <td bgcolor="#e0e0e0"><b>So</b></td> </tr> <tr> <? while($day_arr = $cal->pre_to_post_fetch_days()) { if($day_arr['day_of_week'] == 1) echo '<td bgcolor="#e0e0e0">'.$day_arr[calendarweek].'</td>';?> <td bgcolor="#e0e0e0"><?=$day_arr['day_of_month']?></td> <? echo !$day_arr['day_of_week'] && $day_arr['day_of_month'] < $cal->last_day?"\n </tr>\n <tr>\n":''; } ?> </tr> </table> <? $cal->reset_fetch_days(); echo '<pre>'; print_r($cal); echo '<br><br>'; while($mycal = $cal->pre_to_post_fetch_days()) { print_r($mycal); } echo '</pre>'; /**********************************************************/ // Beim instanziiren der calendar-Klasse kann // als Argument ein Unix-Timestamp übergeben werden. // wenn nicht wird ein Timestamp des aktuellen Datums // verwendet. class calendar { // Eigenschaften der Klasse var $year, $month, $day; var $monthname,$monthname_de,$monthnamecut,$monthnamecut_de; var $calendarweek; var $stamp; var $last_day; // Zählervariable für die Funktion fetch_days() var $cnt = 0; // Adapter für die Umwandlung von einer Woche, welche mit dem // Sonntag beginnt in eine Woche, welche mit dem Montag beginnt var $adapter = array(0 => 6, 1 => 0, 2 => 1, 3 => 2, 4 => 3, 5 => 4, 6 => 5); // Diese Eigenschaften dienen zur Berechnung der Tage, // die zu einer vollständigen Woche aber nicht zum // Monat gehören var $pre = 0, $post = 0; // Konstruktor für die Initialisierung der Eigenschaften function calendar($stamp = 0) { $months_de = array('Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'); $this->stamp = $stamp?$stamp:date('U'); $this->day = date('j',$this->stamp); $this->month = date('n',$this->stamp); $this->year = date('Y',$this->stamp); $this->monthname = date('F',$this->stamp); $this->monthnamecut = date('M',$this->stamp); $this->monthname_de = $months_de[date('n',$this->stamp)-1]; $this->monthnamecut_de = substr($this->monthname_de,0,3); $this->calendarweek = date('W',$this->stamp); $this->last_day = date('t',$this->stamp); $this->pre = $this->adapter[date('w',mktime(0,0,0,date('n',$this->stamp),1,$this->year))]; $this->post = 6 - $this->adapter[date('w',mktime(0,0,0,date('n',$this->stamp),$this->last_day,$this->year))]; } // Methode zur Hilfe für die einfache Konstrucktion // eines Kalenders mit einer while-Schleife function fetch_days() { if(++$this->cnt <= $this->last_day) return $this->day_info(mktime(0,0,0,$this->month,$this->cnt,$this->year)); else return 0; } function pre_to_post_fetch_days() { if(++$this->cnt - $this->pre <= $this->last_day + $this->post) return $this->day_info(mktime(0,0,0,$this->month,$this->cnt - $this->pre,$this->year)); else return 0; } // Zurücksetzen der Eigenschaft "cnt" um ein erneutes aufrufen // der Methode "fetch_days()" zu ermöglichen function reset_fetch_days() { $this->cnt = 0; } // Methode zur Abfrage von Informationen eines Tages function day_info($stamp = 0) { $stamp = $stamp?$stamp:$this->stamp; $weekdays_de = array('Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'); return array('day_of_week' => date('w',$stamp), 'day_of_month' => date('j',$stamp), 'day_of_year' => date('z',$stamp)+1, 'weekday' => date('l',$stamp), 'weekday_de' => $weekdays_de[date('w',$stamp)], 'weekdaycuts' => date('D',$stamp), 'weekdaycuts_de' => substr($weekdays_de[date('w',$stamp)],0,2), 'calendarweek' => date('W',$stamp), 'timestamp' => $stamp); } } ?> ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=19719&edit=1

« previous php.bugs (#21292) next »