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

From: Date: Fri, 06 Aug 2004 16:51:49 +0000
Subject: #19719 [NoF->WFx]: wrong week number of year at year end / beginn
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-64197@lists.php.net to get a copy of this message
ID: 19719 Updated by: hholzgra@php.net Reported By: martin dot adler at continum dot net -Status: No Feedback +Status: Wont fix Bug Type: Calendar related Operating System: FreeBSD 4.3-RELEASE PHP Version: 4.2.2 New Comment: date() is not very clever when it comes to week counts. You should use strftime() instead in this case, this supports the ISO week number conventions as usually used in Germany using the %V and %G format modifiers: strftime("Week %V of Year %G", mktime(12,0,0,1,1,2000)); -> Week 52 of Year 1999 strftime("Week %V of Year %G", mktime(12,0,0,1,1,2004)); -> Week 01 of Year 2004 Previous Comments: ------------------------------------------------------------------------ [2002-10-20 23:30:19] sniper@php.net No feedback was provided. The bug is being suspended because we assume that you are no longer experiencing the problem. If this is not the case and you are able to provide the information that was requested earlier, please do so and change the status of the bug back to "Open". Thank you. ------------------------------------------------------------------------ [2002-10-02 10:45:28] rasmus@php.net Please provide a *short* script that illustrates this problem. Sounds like you could have illustrated this with a 1-line script. ------------------------------------------------------------------------ [2002-10-02 10:43:53] martin dot adler at continum dot 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 (#64197) next »