开发者

Find out date of nth week's monday in PHP?

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 开发者_如何学Pythonand week.


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));


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.


This will solve the problem for you. It mainly derives from Mihail Dimitrov's answer, but simplifies and condenses this somewhat. It can be a one-line solution if you really want it to be.

function getMondaysDate($year, $week) {
  if (!is_numeric($year) || !is_numeric($week)) {
    return null;
    // or throw Exception, etc.
  }

  $timestamp = strtotime("+$week weeks Monday January $year");
  $prettyDate = date('d M Y');
  return $prettyDate;
}

A couple of notes:

  • As above, strtotime("Monday January $year") will give you the timestamp of the first Monday of the year.
  • As above +X weeks will increment a specified date by that many weeks.

You can validate this by trying:

date('c',strtotime('Sunday Jan 2018'));
// "2018-01-07T00:00:00+11:00" (or whatever your timezone is)

date('c',strtotime('+1 weeks Sunday Jan 2018'));
// "2018-01-14T00:00:00+11:00" (or whatever your timezone is)

date('c',strtotime('+52 weeks Sunday Jan 2018'));
// "2019-01-06T00:00:00+11:00"


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


  //To calculate 12 th Monday from this Monday(2014-04-07)
    $n_monday=12;
    $cur_mon=strtotime("next Monday");
    for($i=1;$i<=$n_monday;$i++){
       echo date('Y-m-d', $cur_mon);
       $cur_mon=strtotime(date('Y-m-d', strtotime("next Monday",$cur_mon)));
    }

Out Put

2014-04-07
2014-04-14
2014-04-21
2014-04-28
2014-05-05
2014-05-12
2014-05-19
2014-05-26
2014-06-02
2014-06-09
2014-06-16
2014-06-23
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜