Bug #18532 Updated: date('W'...) returns wrong weeknumbers
| From: | tmus at get2net dot dk | Date: | Thu, 25 Jul 2002 13:19:42 +0000 |
| Subject: | Bug #18532 Updated: date('W'...) returns wrong weeknumbers | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-15190@lists.php.net to get a copy of this message | ||
ID: 18532
Updated by: tmus@get2net.dk
Reported By: tmus@get2net.dk
Status: Open
Bug Type: Date/time related
Operating System: Linux / Windows XP
PHP Version: 4.2.1
New Comment:
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 );
}
---------------------------------------------
Previous Comments:
------------------------------------------------------------------------
[2002-07-24 14:57:12] tmus@get2net.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-24 12:36:19] 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 05:07:50] tmus@get2net.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.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=18532&edit=1