-3

CONTEXT: My client, a local movie theater, runs a Sunday Matinee Special every other Sunday starting with the SECOND SUNDAY every year. So for this year the dates are 1/11, 1/18, 2/8, 2/22, .... [The only exception is the SUNDAY after their film festival which runs the the THIRD FULL WEEK OF OCTOBER, but automating this single exception is a "would-be-nice" item, not essential.]

MY SKILL LEVEL: BEGINNER (I need your help!) I believe I need to use a combination of mktime() and date() but I don't know how to put it together.

WHAT I'VE TRIED: I suspect the answer is a combination of what I see on these three posts:

(1) a do-while loop to get a specific day of the week from a date range

(2) there may be a shortcut for referencing the second sunday in the ACCEPTED ANSWER here, but I'm not sure this helps

(3) MOST RELEVANT(?): Get the First Sunday of Every Month

END RESULT: I want to display the [Month] and [Day] of the next Sunday Matinee (so I want to find and display the first item in the array AFTER the current date). The text will appear like this: "Next: [Month] [Day]"

Make sense? Let me know if I've forgotten anything.

If it's not too much to ask, please explain your code so I (and others?) can learn from this; but I'd be more than grateful for "merely" a straight-up solution.

Many thanks. Debra

UPDATE/PROGRESS: This code will get me the array of Sundays:

$startDate = strtotime("second Sunday of ".date('Y').""); for ($i=0; $i < 27; $i++){ $sundays = date('F j', ($startDate + (($i*14) * 24 * 3600))) . '<br>'; print $sundays; }

NEXT TO FIGURE OUT: write a statement to find in the array of Sundays the first date after the current date.

Community
  • 1
  • 1
DebraG
  • 9
  • 2
  • 3
    Post any code you've tried yourself. – TMH Jan 28 '15 at 09:49
  • @Tom, I only have pieces of this. STEP 1: create the array of Sundays (starting with the 2nd sunday of the year displayed in this format: January 11th). This gets me my start date: $startDate = date("F jS", strtotime("second Sunday of ".date('Y')."")); Now I want to make the array of Sundays: $sundaysArray = array(); I don't know how to write the loop function to get me every other Sunday from the $startDate and stopping when the array has 26 items in it (26 will get me every other sunday for a year). (How does one make line breaks??) – DebraG Jan 29 '15 at 14:37
  • Looks like this language will get me to Sunday-2-weeks: $startDate += (14 * 24 * 3600); But, again, I don't know how to put it all together. – DebraG Jan 29 '15 at 14:43

1 Answers1

0

This is a pretty manual, procedural solution, but it should work.

<?php
$SECS_PER_DAY = 86400;

# Find the first Sunday on or after a given timestamp
function firstSundayOnOrAfter($time) {
  global $SECS_PER_DAY;

  # What day of the week is the given date?
  $wday = date('w', $time);

  if ($wday == 0) {
    # it's already a Sunday
    return $time;
  } 

  return $time + (7 - $wday) * $SECS_PER_DAY;
}

# return an array of timestamps representing 
# (noon on) the special matinee Sundays for the given year
function specialMatineeSundays($year) {

  global $SECS_PER_DAY;

  # When's the film festival?
  $oct1 = mktime(12,0,0,10,1,$year);
  $festivalStart = firstSundayOnOrAfter($oct1);
  $festivalSkip  = $festivalStart + 7 * $SECS_PER_DAY;

  # find the first Sunday of the year
  $jan1 = mktime(12,0,0,1,1,$year);
  $sunday = firstSundayOnOrAfter($jan1);

  # Add a week to start with the second Sunday
  $sunday += 7 * $SECS_PER_DAY;

  # Build up our result list
  $result = [];

  # As long as the Sunday under examination is still the same year, 
  # add it to the list (unless it's the post-festival skip date)
  # and then add two weeks 
  while (date('Y',$sunday) == $year) {
    if ($sunday != $festivalSkip) {
      $result[] = $sunday;
    }
    $sunday += 14 * $SECS_PER_DAY;
  }

  return $result;
}
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • Thanks, @mark, I'm learning a lot from this. Will this work better than the "for-loop" I came up with (see "update/progress" in my first post)? One question about the first section of your code: rather than the first sunday after a given timestamp, I need the sunday inside the array ($result) that follows a given timestamp. Thanks again! – DebraG Jan 29 '15 at 16:05