Use DateTime::setISODate
, where 3rd parameter is day of the week:
$dt = new DateTime;
echo $dt->setISODate(2014, 1, 0)->format('Y-m-d'), "\n"; # 0 = Sunday
echo $dt->setISODate(2014, 1, 1)->format('Y-m-d'), "\n"; # 1 = Monday
echo $dt->setISODate(2014, 1, 6)->format('Y-m-d'), "\n"; # 6 = Saturday
Your example isn't working, because you are using format yyyyWweek
, which is by default Monday. Read about ISO-8601 formats, and you will see that there is format yyyy-Wweek-day
, which you can use like:
$format = sprintf("%d-W%02d-%d", $year, $weeknumber, $weekstarts == 7 ? 0 : $weekstarts);
$first_day_of_week = strtotime($format);
echo "$format returned $first_day_of_week\n";