From: [EMAIL PROTECTED]
Operating system: FreeBSD 4.3-RELEASE
PHP version: 4.2.2
PHP Bug Type: Calendar related
Bug description: wrong week number of year at year end / beginn
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 bug report at http://bugs.php.net/?id=19719&edit=1
--
Try a CVS snapshot: http://bugs.php.net/fix.php?id=19719&r=trysnapshot
Fixed in CVS: http://bugs.php.net/fix.php?id=19719&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=19719&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=19719&r=needtrace
Try newer version: http://bugs.php.net/fix.php?id=19719&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=19719&r=support
Expected behavior: http://bugs.php.net/fix.php?id=19719&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=19719&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=19719&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=19719&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=19719&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=19719&r=dst