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

折月煮酒 提交于 2021-01-29 09:10:19

问题


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:


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



回答2:


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.




回答3:


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




回答4:


  //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



回答5:


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"


来源:https://stackoverflow.com/questions/1741785/find-out-date-of-nth-weeks-monday-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!