php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19719 wrong week number of year at year end / beginn
Submitted: 2002-10-02 10:43 UTC Modified: 2004-08-06 18:51 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: martin dot adler at continum dot net Assigned:
Status: Wont fix Package: Calendar related
PHP Version: 4.2.2 OS: FreeBSD 4.3-RELEASE
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: martin dot adler at continum dot net
New email:
PHP Version: OS:

 

 [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);
  }
}


?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-10-02 10:45 UTC] 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-20 23:30 UTC] 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.


 [2004-08-06 18:51 UTC] hholzgra@php.net
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 


 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Dec 09 11:03:34 2021 UTC