views:

437

answers:

4

I have a simple situation where I have a user supplied week number X, and I need to find out that week's monday's date (e.g. 12 December). How would I achieve this? I know year and week.

+1  A: 

An idea to get you started:

  • take first day of year
  • add 7 * X days
  • use strtodate, passing in "last Monday" and the date calculated above.

May need to add one day to the above.

Depending on the way you are calculating week numbers and the start of the week this may sometimes be out. (i.e. if the monday in the first week of the year was actually in the previous year!)

TEST THIS THOROUGHLY - but I've used a similar approach for similar calcualtions in the past.

Matt Lacey
A: 

Due to reputation restriction i can't post multiple links for details check

http://php.net/manual/en/function.date.php and http://php.net/manual/en/function.mktime.php

you can use something like this : use mktime to get a timestamp of the week : $stamp = mktime(0,0,0,0,<7*x>,) {used something similar a few years back, so i'm not sure it works like this} and then use $wDay = date('N',$stamp). You now have the day of the week, the timestamp of the monday should be

mktime(0,0,0,0,<7*x>-$wDay+1,) {the 'N' parameter returns 1 for monday, 6 for sunday}

hope this helps

Raz
A: 

I believe this can work:

strtotime("+$week week mon jan $year"); // mon = monday, jan = january

Didn't test it though.

Savageman
+1  A: 

Some code based mainly on previous proposals:

$predefinedYear = 2009;
$predefinedWeeks = 47;

// find first mоnday of the year
$firstMon = strtotime("mon jan {$predefinedYear}");

// calculate how much weeks to add
$weeksOffset = $predefinedWeeks - date('W', $firstMon);

// calculate searched monday
$searchedMon = strtotime("+{$weeksOffset} week " . date('Y-m-d', $firstMon));
Mihail Dimitrov