Finally, this accounts for calendar week, year, whether it is a leap year, how many calendar weeks are in the year (and previous year for going backwards.)
Hope this helps someone out. Code critiques are welcome!
<?php
if (empty($yr)) //passed through links
{
$uYr = date("Y");
}
else
{
$uYr = $yr;
}
//checks how many calendar weeks in a year
function weeknumb_check($uYr)
{
$jan1 = getdate( mktime( 0, 0, 0, 1, 1, $uYr ) );
//check if year is a leap-year
if (date("L", mktime(0,0,0,1,1,$uYr)))
{
$s = 1; //yes it is a leap year
}
else
{
$s = 0;
}
//check how many weeks the year has
if ( ( $jan1['wday'] == 3 && $s == 1 ) || $jan1['wday'] == 4 )
{
return $weeks = 53;
}
else
{
return $weeks = 52;
}
}//end of function
if (!empty($showkw))//passed through links
{
$week = $showkw;
}
else
{
$week = date("W");
}
//set weeks for this year
$weeks = weeknumb_check($uYr);
if ($showkw == $weeks)//set values for kw 1 and next year
{
$uYear = $uYear + 1 ;
$nWeek = 1;
}
else
{
$nWeek = $week + 1;
}
if ($showkw == 1)//set values for previous year
{
$pWeek = $weeks;
}
else
{
$pWeek = $week - 1;
}
$jan1_day_of_week = date('w',$jan1);
$start_date = strtotime(($week-1)." weeks 1/1/".$uYr);
// 0 - Sunday, 1 - Monday, ..., 6 - Saturday
$day_of_week = date('w',$start_date);
// 1 - Monday, ..., 7 - Sunday
if( $day_of_week==0 )
$day_of_week = 7;
// finding timestamp of Monday
if ($day_of_week > 4)//if jan 1 is on a fri, sat or sun than it is in last year's calendar weeks
{
$start_date = $start_date - (24*60*60*($day_of_week-1)) + (24*60*60*7);
}
else
{
$start_date = $start_date - 24*60*60*($day_of_week-1);
}
$Month = date('M',$start_date+24*60*60*$i);
$Year = date('Y',$start_date+24*60*60*$i);
echo "<table border=\"1\">";
if ($showkw == 1)
{
//check if previous year is 53 or 52 weeks
$weeks = weeknumb_check($uYr - 1);
$pWeek = $weeks;
echo "<tr><td><a href=\"" . $PHP_SELF . "?yr=" . ($uYr - 1) . "&showkw=" . $pWeek . "\"><small><<</small></a></td><td colspan=\"5\" align=\"CENTER\"><b>$Month $Year KW $week </b></td><td><a href=\"" . $PHP_SELF . "?yr=" . $uYr . "&showkw=" . $nWeek . "\"><small>>></small></a></td></tr><BR>";
}
elseif ($showkw == $weeks)
{
$pWeek = $week - 1;
echo "<tr><td><a href=\"" . $PHP_SELF . "?yr=" . $uYr . "&showkw=" . $pWeek . "\"><small><<</small></a></td><td colspan=\"5\" align=\"CENTER\"><b>$Month $Year KW $week </b></td><td><a href=\"" . $PHP_SELF . "?yr=" . ($uYr + 1) . "&showkw=" . $nWeek . "\"><small>>></small></a></td></tr><BR>";
}
else
{
echo "<tr><td><a href=\"" . $PHP_SELF . "?yr=" . $uYr . "&showkw=" . $pWeek . "\"><small><<</small></a></td><td colspan=\"5\" align=\"CENTER\"><b>$Month $Year KW $week </b></td><td><a href=\"" . $PHP_SELF . "?yr=" . $uYr . "&showkw=" . $nWeek . "\"><small>>></small></a></td></tr><BR>";
}
echo "<tr><td>Mon</td><td>Tues</td><td>Weds</td><td>Thurs</td><td>Fri</td><td>Sat</td><td>Sun</td></tr>";
echo "<tr>";
for( $i=0; $i<7; $i++ ) {
$display_date = date('d.m',$start_date+24*60*60*$i);
//$dispDay = date('d',$start_date+24*60*60*$i);
//$dispMonth = date('m',$start_date+24*60*60*$i);
echo "<td>$display_date</td>";
}
?>
-Sonya