|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-10-02 10:43 UTC] 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);
}
}
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 20:00:01 2025 UTC |
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