tags:

views:

416

answers:

2

I'm kind of at a loss here. It seems as though somehow my code is missing a whole week at the end of 2009 and I've tried a couple different things.

My base function to get the start and end date for a week is below. Given a Year, Week and Day of the Week it gives you a date.

function datefromweeknr($aYear, $aWeek, $aDay)
    {
        $Days=array('xx','ma','di','wo','do','vr','za','zo');
        //xx = Current Sun, ma = Mon ..... zo = Sun of the next Week
        $DayOfWeek=array_search($aDay,$Days); //get day of week (1=Monday)
        $DayOfWeekRef = date("w", mktime (0,0,0,1,4,$aYear)); //get day of week of January 4 (always week 1)
        if ($DayOfWeekRef==0){
            $DayOfWeekRef=7;
        }
        $ResultDate=mktime(0,0,0,1,4,$aYear)+((($aWeek-1)*7+($DayOfWeek-$DayOfWeekRef))*86400);
        return $ResultDate;
    }

Seemed to work completely fine until I realized that I was missing the week of December 27th 2009 to January 2nd 2010.

echo '<table border="1">';
for($i = 1; $i < 53; $i++){
    if($i < 10){
        $w = '0'.$i.'1';
    }
    else{
        $w = $i.'1';
    }
    echo '<tr><td>Week#'.$i.' </td><td> '.date("Y-m-d",datefromweeknr(2009,$i,"xx")).' </td><td> '.date("Y-m-d",datefromweeknr(2009, $i,"za")).'</td><td> Week = '.date("W: Y-m-d",strtotime("2009W$w")).'  </td></tr>';

}

echo '</table>';

It seems the 52nd week of the year ends on 2009-12-26 and the 1st week of the new year starts on 2010-01-03. I'm losing a whole week, No Bueno!

Anyone know what I'm doing wrong or can point me to a fool proof way of supplying a week number and a year to get me the start and end date of that week without losing any days in the process?

A: 

Check here: http://www.onlineconversion.com/day_week_number.htm If you enter 29 december 2009, so see that US and ISO/Europe give different week numbers (resp. 52 and 53). Could this be related to your problem? Which standard do you dates conform too?

Edit: From http://www.epochconverter.com/epoch/weeknumbers.php : Week number according to the ISO-8601 standard, weeks starting on Monday. The first week of the year is the week that contains that year's first Thursday. The highest week number in a year is either 52 or 53.

Jimmy Shelter
I am using ISO 8601 in a timekeeping application. and the problem is that there are 53 weeks this year. Now how to figure out which years have "leap weeks"! Thanks Jimmy
mmundiff
A: 

Your question remainded me of a bug comment I read today on php.net:

In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions.

Which is irrevelent for now but I would suggest you to replace your calculations in datefromweeknr with strtotime calls. I'm pretty sure strtotime will fix your calculation bug.

So you could use something like:

strtotime('last Monday', $timestamp);
AlexV