php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18532 date('W'...) returns wrong weeknumbers
Submitted: 2002-07-24 05:07 UTC Modified: 2002-09-10 05:58 UTC
From: tmus at get2net dot dk Assigned: georg (profile)
Status: Closed Package: Date/time related
PHP Version: 4.2.1 OS: Linux / Windows XP
Private report: No CVE-ID: None
 [2002-07-24 05:07 UTC] tmus at get2net dot dk
There is a problem with the date('W'...) function in PHP 4.2.1...

The ISO standard says that week 1 is the week that has the first
thursday of the gregorian year, making the last week the week with the
last thursday of the year (more/better info here
http://www.merlyn.demon.co.uk/weekinfo.htm#WkNo )

But anyway, try making the following test...

print date("r (W)",1041289200)."<br>\n"; // Tue, 31 Dec 2002 00:00:00
+0100 (53)
print date("r (W)",1041375600)."<br>\n"; // Wed,  1 Jan 2003 00:00:00
+0100 (1)

So any way You decide to put it, at least the same week should not have
two different numbers depending on the timestamp.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-24 12:36 UTC] georg@php.net
date uses iso8601 standard which supports long years (53 weeks).

the first week of the ISO calendar year can begin as late as 4 January and as early as 29 December of the previous year. Likewise, the last week of the ISO calendar year can end as early as 28 December and as late as 3 January of the following year.

 [2002-07-24 14:57 UTC] tmus at get2net dot dk
I'm sorry, but did you read my bug report before closing it as bogus..?

it returns Week 53 for TUESDAY 31 Dec. and Week 1 for the day AFTER that - This is incorrect!!!

The way I read the ISO date specification, it is NOT possible for the same week MON-SUN to have two different week numbers!!!

And the ISO specifies that a WEEK is STARTING on monday and ENDING on sunday, so howcome the weeknumber changes between Tuesday and Wednesday...

Please just try the code i sent you guys!
 [2002-07-25 09:19 UTC] tmus at get2net dot dk
I made a small C function that's supposed to carry out the calculation(based on the W handling code of the php datetime.c file)

Please notice that I am in no way a math wiz and that this routine is likely optimizable and maybe even buggy. However, to the depth of my tests, it seems to do the trick...

I provide it here as a "it-could-be-done-something-like-this" kinda thing... I'm sorry I'm unable to write a proper patch for you to test, but I have no working compiler at the moment.

Please take it for a spin if you like or use it for inspiration. Also check out the link mentioned elsewhere in the report. He has a lot of sample script etc. that may provide you with useful information...

Here goes the code
-----------------------------------
/*********************************************************
 *
 * tm_wday: day of week(0=sun - 6=sat)
 * tm_yday: day of year(0=jan 1. - 364 or 365(leap years)
 * tm_leap: current year leap flag(0 if not leap - 1 if leap)
 * ly_leap: last years leap flag(0 if not leap - 1 if leap) 
 *
 *********************************************************/
int week(int tm_wday, int tm_yday, int tm_leap, int ly_leap){
   
   int yd, fd, ld, wk, wd;

   wd = ( tm_wday == 0 ) ? 7 : tm_wday; 
   yd = tm_yday + 1;                    
   fd = ( 7 + ( wd - yd ) % 7 ) % 7;    
   ld = ( fd + ( 364 + tm_leap ) %7 ) %7;   
   wk = ( ( yd + fd - 1 ) / 7 ) + 1;
   if ( fd > 3 ) wk--;
   if ( ld < 3 && yd > ( ( 364 + tm_leap ) - ld ) ) wk = 1;
   if( wk == 0 ) wk = week( tm_wday, 364+ly_leap, ly_leap, 0 );

   return( wk );

}
---------------------------------------------
 [2002-07-25 11:45 UTC] pguillot at paanjaru dot com
%W returns some 0th week of the year :
$deb  = mktime( 0, 0, 0, 1, 1, '2002');
print 'Week test for day '. $deb ."\n";
print 'for day '. strftime( '%d-%m-%Y', $deb) ."\n"; 
print 'strftime %Y-%W gives ';
print strftime( '%Y-%W', $deb ) . "\n";

When run, it gives 
Week test for day 1009839600 (01-01-2002)
strftime %Y-%W gives 2002-00

So, this will lead to a lot of problems when using week
computations ...

you can use 
function D2YWeeknum( $date ) {
//
// pg 20020719 pguillot@paanjaru.com
// for intentis.net
// Inspired Algorithm used:From Klaus Tondering's Calendar document (The Authority/Guru)
// http://www.tondering.dk/claus/calendar.html
//
// It's not yet possible in the port to select the week start.

   $supplemental = "";
   $this_date    = getdate( $date );
   $year         = $this_date['year'];
   $month        = $this_date['mon'];
   $day          = $this_date['mday'];

   $a    = floor( ( 14 - $month ) / 12 );
   $y    = $year + 4800 - $a;
   $m    = $month + 12 * $a - 3;
   $J    = $day + floor( ( 153 * $m + 2 ) / 5 ) + 365 * $y + floor( $y / 4) - floor( $y / 100 ) + floor( $y / 400 ) - 32045;
   $d4   = ( ( ( $J + 31741 - ( $J % 7 ) ) % 146097) % 36524) % 1461;
   $L    = floor( $d4 / 1460);
   $d1   = ( ( $d4 - $L ) % 365 ) + $L;
   $week = floor( $d1 / 7 ) + 1;
   if ( $week < 10 ) {
      $supplemental = '0';
   }
   return( $year .'-'. $supplemental . $week );
}

wich will gives you a correct number ...
 [2002-07-26 04:06 UTC] georg@php.net
Yes, you're right. Looks like the code is a little bit buggy. I'll fix that.
 [2002-07-26 12:51 UTC] georg@php.net
This bug has been fixed in CVS. You can grab a snapshot of the
CVS version at http://snaps.php.net/. In case this was a documentation 
problem, the fix will show up soon at http://www.php.net/manual/.
In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites.
Thank you for the report, and for helping us make PHP better.


 [2002-09-10 04:54 UTC] tmus at get2net dot dk
I was just wondering why the fix does not seem to be in the 4.2.3 release - am I incorrect or what? I have not installed it yet (prod system) but the source code for the 'W' function does not appear to have changed?!?
 [2002-09-10 05:58 UTC] hholzgra@php.net
it's only changed in the 4.3(HEAD) branch
and wasn't merged back into the 4.2 branch
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 07:01:27 2024 UTC