4

I found this PHP code credit to creator and want to implement it a different way:

The goal is to have the code automatically adjust the following statement to be the second Saturday of every month from now until eternity:


Next membership meeting: Saturday, MONTH, DAY, YEAR, 11 a.m. to noon.

As in: "Saturday, February 12, 2011, 11 a.m. to noon."


I am no PHP guru, can someone kindly edit it to work?

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

     ?>
Mark
  • 55
  • 1
  • 4

3 Answers3

13

How about you save about 5000 lines and try this

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

Edit

Ok, I lied in the comments, I will write it for you.

<?

$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);
Jonathan
  • 1,955
  • 5
  • 30
  • 50
profitphp
  • 8,104
  • 2
  • 28
  • 21
  • 1
    Interesting - I also found strtotime('+1 week sun nov 2009'); to get the second Sunday of November 2009 in the user notes. – HorusKol Feb 02 '11 at 22:41
  • @HorusKol Ya, thats actually going to be closer to what he needs in actuality, since you can specify future months that way, rather than mine which just goes off the current month. Same concept though. – profitphp Feb 02 '11 at 22:43
  • 1
    +1 for simplicity, as long as `$date` is only `Y-m` or atleast the first of the month. Otherwise it will return the 2nd Saturday starting from that $date – Fanis Hatzidakis Feb 02 '11 at 22:45
  • So what happens on February 13, 2011? I need it to know it's past the 2nd Saturday in February, and automatically change to read "March 12, 2011" an so forth. – Mark Feb 02 '11 at 22:46
  • @mark Check if the date has passed, and then recalculate, I'm not going to code it for you, the hard part is done. – profitphp Feb 02 '11 at 22:48
  • just a small mistake: must read `strtotime("next month")` instead of `"next month"` only (in line 7; my edit has been rejected telling me to address the author in a comment instead...) – loptrinho Mar 31 '15 at 09:31
  • if the specified weekday is first of month and you search e. g. for the second, you will get the third (e. g. "December 2015 second tuesday") – Jonathan Nov 23 '15 at 09:26
0

I needed to find the DTS date range, so here's how I did it:

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

And of course you can replace the month with some simple coding as well.

phoenix
  • 1,629
  • 20
  • 11
0

You could change the function a little like this:

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

That should get you the next 12 meetings after today...

HorusKol
  • 8,375
  • 10
  • 51
  • 92