【问题标题】:Find second and fourth saturday's of the month查找本月的第二个和第四个星期六
【发布时间】:2013-06-11 06:42:06
【问题描述】:

如何查找每月的第 2 和第 4 个星期六。 我写了这些行:-

echo "may 2nd sat ".date('d', strtotime('may 2013 second saturday'));
echo '<br/>may 4th sat '.date('d', strtotime('may 2013 fourth saturday'));                                  
echo '<br/>june 2nd sat '.date('d', strtotime('june 2013 second saturday'));
echo '<br/>june 4th sat '.date('d', strtotime('june 2013 fourth saturday'));

它给出以下输出:-

may 2nd sat 11
may 4th sat 25
june 2nd sat 15
june 4th sat 29

它在may 月份给出正确答案,但不是june 2013,在jun 1013 第2 和第4 个星期六应该分别是8 和22。我该如何解决这个问题。

【问题讨论】:

    标签: php datetime php-5.4


    【解决方案1】:

    我不确定为什么你的不起作用,但试试这个,它对我有用:

    echo '<br/>june 2nd sat '.date('d', strtotime('second sat of june 2013'));
    echo '<br/>june 4th sat '.date('d', strtotime('fourth sat of june 2013'));
    

    它基于PHP manual page 中的"first sat of July 2008" 示例

    【讨论】:

    【解决方案2】:

    我不知道为什么会导致此错误,但是我找到了如何获得正确答案的解决方案

    echo date('d',strtotime('+1 week sat may 2013')).'<BR>';
    echo date('d',strtotime('+3 week sat may 2013')).'<BR>';
    echo date('d',strtotime('+1 week sat june 2013')).'<BR>';
    echo date('d',strtotime('+3 week sat june 2013')).'<BR>';
    

    此解决方案工作正常并显示正确的结果。

    输出:

    11
    25
    08
    22
    

    【讨论】:

      【解决方案3】:

      那么您必须运行低于 5.2.7 的 PHP 版本,如 manual states

      在 PHP 5 5.2.7 之前的版本中,如果该工作日是该月的第一天,则请求该月中给定工作日的给定事件会错误地将一周添加到返回的时间戳。这已在 5.2.7 及更高版本中得到纠正。

      因此,如果您可以更新您的 PHP 版本,那将是最好的解决方案。否则你可以检查类似的东西。

      <?php
      function showDay($month, $year, $day, $count)
      {
        $list = array(1=>'first',2=>'second',3=>'third',4=>'fourth',5=>'fifth');
        $first = date('d', strtotime($month . ' ' . $year . ' ' . $list[1] .' '.$day));
        $show= ($first>7) ?  $count-1 : $count;
        return date('d', strtotime($month . ' ' . $year . ' ' . $list[$show] .' '.$day));
      }
      
      echo '<br/>june 2nd sat '.showDay('june', 2013, 'saturday', 2);
      ?>
      

      【讨论】:

      • OP 被标记为php-5.4。但无论如何,这是有用的信息,很高兴知道
      【解决方案4】:

      我通常不依赖一根绳子。定制功能怎么样?

      function getSaturdayDay($year, $month, $position) {
          $firstDay = date('w', mktime(0, 0, 0, $month, 1, $year));
          $diff = 6 - $firstDay;
      
          return 1 + $diff + $position * 7;
      }
      

      并在你的上下文中使用它

      echo "may 2nd sat " . getSaturdayDay(2013, 5, 1);
      echo '<br/>may 4th sat ' . getSaturdayDay(2013, 5, 3);                          
      echo '<br/>june 2nd sat ' . getSaturdayDay(2013, 6, 1);
      echo '<br/>june 4th sat ' . getSaturdayDay(2013, 6, 3);
      

      【讨论】:

      • 迄今为止最可靠的答案
      【解决方案5】:

      这些天我首选的方法是扩展 PHP 的 DateTime object:-__

      class MyDateTime extends DateTime
      {
          /**
           * Returns a MyDateTime object set to 00:00 hours on the nth occurence
           * of a given day of the month
           *
           * @param string $n nth day required, eg first, second etc
           * @param string $day Name of day
           * @param mixed $month Month number or name optional defaults to current month
           * @param mixed $year optional defaults to current year
           *
           * @return MyDateTime set to last day of month
           */
          public function nthDayOfMonth($n, $day, $month = null, $year = null)
          {
              $timestr = "$n $day";
              if(!$month) $month = $this->format('M');
              $timestr .= " of $month $year";
              $this->setTimestamp(strtotime($timestr));
              $this->setTime(0, 0, 0);
              return $this;
          }
      }
      $dateTime = new MyDateTime();
      echo $dateTime->nthDayOfMonth('second', 'Sun', 'Jul', 2011)->format('Y-m-d');
      

      输出:-

      2011-07-10
      

      【讨论】:

        【解决方案6】:

        查找每月的第二个和第四个星期六:

        private bool FindSecondSaturdayFourthSaturday()
        {      
            bool IsHoliday = false;
            DateTime TodaysDate = DateTime.Today;            
            DateTime SecondSaturday;
            DateTime FourthSaturday;
        
            //SecondSaturday will be after 8
            SecondSaturday = Enumerable.Range(8, 7)
                          .Select(item => new DateTime(TodaysDate.Year, TodaysDate.Month, item))
                          .Where(date => date.DayOfWeek == DayOfWeek.Saturday)
                          .Single();
            //Lsat Saturday will be after 22 
            FourthSaturday = Enumerable.Range(22, 7)
                         .Select(item => new DateTime(TodaysDate.Year, TodaysDate.Month, item))
                         .Where(date => date.DayOfWeek == DayOfWeek.Saturday)
                         .Single();
        
            return IsHoliday;
        }
        

        【讨论】:

          【解决方案7】:

          每年获得第二个和第三个星期六

          public function getSndFthSaturday()
              {
                  $now = strtotime("01-01-2019");
                  $end_date = strtotime("31-12-2019");
                  $this->calculate($now, $end_date);
              }
          
              public function calculate($now, $end_date)
              {
                  while (date("Y-m-d", $now) != date("Y-m-d", $end_date))
                  {
                      $day_index = date("w", $now);
                      $day_indexD = floor((date("d", $now) - 1)/ 7);
          
                      if ($day_index == 6 && ($day_indexD == 1 || $day_indexD == 3)) {
                          $now1 = date("Y-m-d", $now);
                          print_r($now1);
                          echo "</br>";
                      }
                      $now = strtotime(date("Y-m-d", $now) . "+1 day");
                  }
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-08-06
            • 1970-01-01
            • 1970-01-01
            • 2021-10-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-11-23
            相关资源
            最近更新 更多