PHP的月份第二个星期六

时间:2011-02-02 22:28:56

标签: php function date

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

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

<小时/> 下一次会员会议:周六,每月,每年,每年,上午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);

     ?>

3 个答案:

答案 0 :(得分:10)

如何节省约5000行并尝试此

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

修改

好的,我在评论中撒谎,我会为你写的。

<?

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

答案 1 :(得分:0)

你可以改变这个功能:

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次会议......

答案 2 :(得分:0)

我需要找到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}"));

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

相关问题