|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Sun Mar 15 16:00:01 2026 UTC |
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 ); } ---------------------------------------------%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 ...