找出 PHP 中第 n 周星期一的日期?

发布于 2024-08-11 04:02:17 字数 76 浏览 3 评论 0原文

我有一个简单的情况,用户提供了第 X 周,我需要找出该周的星期一日期(例如 12 月 12 日)。我将如何实现这一目标?我知道年份和星期。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

逆蝶 2024-08-18 04:02:17

一些代码主要基于之前的提案:

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

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));
蝶…霜飞 2024-08-18 04:02:17

一个入门的想法:

  • 将一年的第一天
  • 添加 7 * X 天,
  • 使用 strtodate,传入“上星期一”和上面计算的日期。

可能需要在上述基础上添加一天。

根据您计算周数和周开始的方式,有时可能会出现错误。 (即,如果一年第一周的星期一实际上是上一年的星期一!)

彻底测试这一点 - 但我过去曾使用类似的方法进行类似的计算。

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.

夏有森光若流苏 2024-08-18 04:02:17

这将为您解决问题。它主要源自 Mihail Dimitrov 的回答,但有所简化和浓缩。如果您确实愿意,它可以是一种单线解决方案。

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

一些注意事项:

  • 如上所述,strtotime("Monday January $year") 将为您提供一年中第一个星期一的时间戳。
  • 如上所述,+X 周会将指定日期增加该周数。

您可以通过尝试验证这一点:

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"

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"
温柔戏命师 2024-08-18 04:02:17

由于声誉限制,我无法发布多个链接
有关详细信息,请检查

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

你可以使用这样的东西:
使用 mktime 获取本周的时间戳: $stamp = mktime(0,0,0,0,<7*x>,) {几年前使用过类似的东西,所以我不确定它是这样工作的}
然后使用 $wDay = date('N',$stamp)。现在您已经知道了星期几,星期一的时间戳应该是

mktime(0,0,0,0,<7*x>-$wDay+1,) { 'N' 参数返回 1 表示星期一,周日 6}

希望这有帮助

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

不气馁 2024-08-18 04:02:17
  //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)));
    }

输出

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
  //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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文