Q:

Generate select field with options to select calendar weeks

i want to create a select field where you can select all calendar weeks between the last 5 and the next 5.
It is important, to notice, that some of the last 5 weeks are in 2013 and some in 2014.
My starting point is:
function generateSelect($calendarweek)
{
  $this_cw = $calenderweek;
  $return = "<select>";

  list($y, $m, $d) = explode('-', date('Y-m-d'));

  for ($i=-5; $i<=5; $i++) 
  {
    $cw = date('W', mktime(0, 0, 0, $m, $d + 7*$i, $y));
    $return .= "<option>".$cw." HERE I WANT THE CORRESPONDING YEAR</option>";
  }

  $return .= "</select>";

  return $return;
}

My main problem is the corresponding year.
And I don't know if my solution is that good.
$calendarweek is the current calenderweek

A:

The answer to this question is almost identical to this one:
$weeks = 5; # number of weeks before and after current week
$dt = new DateTime;
$dt->modify("-$weeks week");
for ($i = 0; $i <= $weeks * 2; $i++) {
    echo $dt->format('W o'), "\n";
    $dt->modify('+1 week');
}

demo
or just change date formating in your example from $cw = date('W', to $cw = date('W o',, where format o is ISO-8601 year number, read more here.

