Figure out what day each array starts and ends on?
<p>I have an array of words and each word is valid from 5PM that day until 5PM the next day, then the next word is valid. <strong>Except on weekends</strong> in which a word for Friday lasts until Monday at 5PM.</p>

<p>Now what I am trying to do is determine if the users inputted word is valid for that period of time. I have a working example which works fine, but my problem is the weekends mess everything up. And I can't seem to figure out how to make them work.</p>

<p>I got a function to figure out how many weekends occur between two timestamps</p>

<pre><code>// Figure out how many weekends occured between two dates
function weekends($date1, $date2) {
    $d1 = new DateTime('@' . $date1);
    $d2 = new DateTime('@' . $date2);

    // Compare the two dates
    $diff = $d1-&gt;diff($d2);

    // Get number of days between them
    $days = $diff-&gt;format('%a');

    // Find out day of the week we start on
    $start = $d1-&gt;format('w');

    // Verify we are not on a weekend
    if($start == 0 || $start == 6)
        return false;

    // Number of days until weekend
    $until_weekend = 7 - $start; // (6 is Saturday but we are counting today)

    // Find out how many days are left between the first weekend and the end
    $left = $days - $until_weekend;

    // How many weekends
    $weekends = floor($left / 7) + 1;

    return $weekends;
}
</code></pre>

<p>And then I got a function to determine if the word is valid for that date range</p>

<pre><code>// Keyword Validation
function keyword_validate($keywords = array()) {
    if(empty($keywords)) return false;

    // Break into values
    $keyword = $keywords['keyword'];
    $contest = $keywords['contest'];
    $keywords = $contest['keywords'];

    // Get some dates
    $now = new DateTime('now');
    $start = new DateTime('@' . $contest['start_time']);
    $s1 = new DateTime('@' . $contest['start_time']); // value for timestamps
    $s2 = new DateTime('@' . $contest['end_time']); // value for timestamps
    $end = new DateTime('@' . $contest['end_time']);

    // Verify keyword exists
    if(in_array($keyword, $keywords) === FALSE)
        return false;

    // Get index
    $index = array_search($keyword, $keywords);

    // See if we somehow got more then one keyword
    if(count($index) != 1)
        return false;

    // get number of weekends
    $weekends = weekends($start-&gt;getTimestamp(), $end-&gt;getTimestamp());

    // Based on index get the two container timestamps
    $s = $s1-&gt;add(new DateInterval('P' . $index + $weekends . 'D'));

    // Verify start doesn't equal Friday or a Weekend

    $e = $s2-&gt;add(new DateInterval('P' . $index + $weekends + 1 . 'D'));

    if($s === FALSE || $e === FALSE)
        return false; // Something really bad happened

    // Based on dates find out if the keyword works.

    print $s-&gt;getTimestamp();
    print $e-&gt;getTimestamp();
    // Get the current time

}
</code></pre>

<p>As you can seem the keyword function doesn't work atm. What I am doing atm is matching the index of the keyword to day, but if it is say Tuesday (2 weekends after) how can I make it so the index is increased by 4. Sorry if this doesn't make any sense, I'm a little lost.</p>
