【问题标题】:Find out date of nth week's monday in PHP?在 PHP 中找出第 n 周的星期一的日期?
【发布时间】:2009-11-16 12:06:02
【问题描述】:

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

【问题讨论】:

    标签: php date week-number


    【解决方案1】:

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

    $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】:

      一个让你开始的想法:

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

      可能需要在上面增加一天。

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

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

      【讨论】:

        【解决方案3】:

        由于信誉限制,我无法发布多个链接 详情查看

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

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

        mktime(0,0,0,0,-$wDay+1,) {'N'参数返回1表示周一,6表示周日}

        希望对你有帮助

        【讨论】:

          【解决方案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)));
              }
          

          输出

          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】:

            这将为您解决问题。它主要来自 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"
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2014-02-11
              • 2012-11-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多