【问题标题】:Second Saturday of Month Function in PHPPHP中的每月第二个星期六函数
【发布时间】:2011-06-20 07:42:54
【问题描述】:

我找到了这个 PHP 代码 credit to creator 并想以不同的方式实现它:

目标是让代码自动将以下语句调整为从现在到永远每个月的第二个星期六:


下次会员会议:星期六,MONTH,DAY,YEAR,上午 11 点到中午。

如:“星期六,2011 年 2 月 12 日,上午 11 点到中午。”


我不是 PHP 大师,有人可以编辑它吗?

<?php

     function nextMeeting($nextMonth = false) {

     $day=date("j");
     $month=date("n");
     $year=date("Y");

     if ($nextMonth) {
             $day=1;
             if ($month == 12) {
                     $month=1;
                     $year++;
             } else {
                     $month++;
             }
     }

     $dayofweek=date("w");
     $firstOfMonth=date("w",mktime(0, 0, 0, $month , 1, $year ));


     // figure out what date is the second Saturday of the month
     if ( $firstOfMonth > 0 ) {
             $firstSunday= 8 - $firstOfMonth;
     } else {
             $firstSunday= 1;
     }

     $firstSundayDate=date("D",mktime(0, 0, 0, $month ,  
     $firstSunday, $year));

     // figure out what date the third monday of the month is
     if ( $firstOfMonth > 1) {
             $offSet = 8 - $firstOfMonth;
     } elseif ( $firstOfMonth == 0 ) {
             $offSet=1;
     } else {
             $offSet=0;
     }
     $thirdMonday= 15 + $offSet;
     $thirdMondayDate=date("D",mktime(0, 0, 0, $month ,  
     $thirdMonday, $year));

     // lets see which of these dates now applies
     if ($day <= $firstSunday) {
             // we didn't miss the first meeting
             $nextMeeting=$firstSunday;
             $nextMeetingDate=mktime(0, 0, 0, $month ,  
             $nextMeeting, $year);
     } elseif ( ($day > $firstSunday) && ($day <= $thirdMonday) ) {
             // we missed the first meeting of the month, but can still make the second
             $nextMeeting=$thirdMonday;
             $nextMeetingDate=mktime(0, 0, 0, $month ,  
             $nextMeeting, $year);
     } else {
             // we need to wait until next month
             $nextMeetingDate=nextMeeting(1);
             $nextMeeting=nextMeeting(1);
     }

     return $nextMeetingDate;

     }

     $meeting=nextMeeting();

     echo "Next membership meeting is on " . date('l dS \of F Y', $meeting);

     ?>

【问题讨论】:

    标签: php function date


    【解决方案1】:

    你如何保存大约 5000 行并试试这个

    <?
    echo date('Y-m-d', strtotime('second saturday of february 2011'));
    

    编辑

    好吧,我骗了cmets,我给你写。

    <?
    
    $now=date("U");
    $monthyear=date("F Y");
    $secondsat=date('U', strtotime($monthyear.' second saturday'));
    if ($now>$secondsat) {
        $monthyear=date("F Y", "next month");
        $secondsat=date('U', strtotime($monthyear.' second saturday'));     
    }
    
    echo date("m/d/Y",$secondsat);
    

    【讨论】:

    • 有趣 - 我还发现 strtotime('+1 week sun nov 2009');在用户注释中获取 2009 年 11 月的第二个星期日。
    • @HorusKol Ya,这实际上将更接近他实际需要的内容,因为您可以通过这种方式指定未来的月份,而不是我的当前月份刚刚结束。相同的概念。
    • +1 为简单起见,只要 $date 仅是 Y-m 或至少是本月的第一天。否则它将返回从该 $date 开始的第二个星期六
    • 那么 2011 年 2 月 13 日会发生什么?我需要它知道它已经过了 2 月的第二个星期六,并自动更改为“2011 年 3 月 12 日”等等。
    • @mark 检查日期是否过了,然后重新计算,我就不给你码了,难的部分已经完成了。
    【解决方案2】:

    你可以像这样改变函数:

    function nextMeeting($date = null)
    {
      $day   = date("j", $date);
      $month = date("n", $date);
      $year  = date("Y", $date);
    
      // remove the nextMonth bit
    
      $dayofweek=date("w", $date);
      $firstOfMonth=date("w",mktime(0, 0, 0, $month , 1, $year ));
    
      // all the same between here and the end
    
      return $nextMeetingDate;
    }
    
    for ($i = 0; $i < 12; $i++) {
      $date = mktime(0, 0, 0, date('m') + $i, date('d'), date('y'));
    
      echo "Next membership meeting is on " . date('l dS \of F Y', nextMeeting($date));
    }
    

    这应该会让你在今天之后的下 12 场会议...

    【讨论】:

      【解决方案3】:

      我需要找到 DTS 日期范围,所以我是这样做的:

      $year = date("Y");
      $dtsStart = date('Y-m-d 02:00:00', strtotime("Second Sunday Of March {$year}"));
      $dtsEnd = date('Y-m-d 02:00:00', strtotime("First Sunday Of November {$year}"));
      

      当然,您也可以用一些简单的编码来替换月份。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多