Example #1
0
    </table>
    <hr>
    <h2>Repeating Date Generator</h2>
      <form action="repeattest.php">
	Repeating Date: <input type=text name=repeat value="<?php 
print $_REQUEST["repeat"];
?>
">
	<input type=submit>
	<a href="explain/repeathelp.php" target="_BLANK" onclick="window.open('explain/repeathelp.php', 'explain', 'width=600, height=500, menubar=no, status=no, location=no, toolbar=no, scrollbars=yes'); return false;">About dates...</a>
      </form>
<?php 
# Test the date repeat functions
if ($_REQUEST["repeat"]) {
    print "<p><h2>Results for '{$_REQUEST['repeat']}'</h2>";
    $tokens = datetokens($_REQUEST["repeat"]);
    print "<table>";
    print "<tr><td>Tokens:</td><td>[";
    for ($i = 1; $tokens[$i]; $i++) {
        print ($i == 1 ? "" : ", ") . tokenstr($tokens[$i]);
    }
    print "]</td></tr>";
    $tokens = datealso($tokens);
    print "<tr><td>Tweaked tokens:</td><td>[";
    for ($i = 1; $tokens[$i]; $i++) {
        print ($i == 1 ? "" : ", ") . tokenstr($tokens[$i]);
    }
    print "]</td></tr>";
    $rules = daterules($tokens);
    print "<tr><td valign=top>Rules:</td><td>";
    dumprules($rules);
Example #2
0
function repeatdates($str)
{
    # Create an empty array
    $daylist = array();
    $d = 1;
    # Parse the dates
    $tokens = datetokens($str);
    $tokens = datealso($tokens);
    $rules = daterules($tokens);
    # Scan actual dates, starting with today and going forward 364 days
    # or until a 180 gap is detected, looking for days that meet the
    # parsed requirements.
    $date = time();
    $tm = getdate($date);
    $date += (12 - $tm["hours"]) * 3600;
    # Roughly noon today
    for ($gap = -999, $i = 0; $gap < 180 && $i < 365; $i++) {
        if (dateinstance($rules, $date)) {
            # Found one!  Append it to the list.
            $daylist[$d++] = array(timestamp => $date, sqldate => date("Y-m-d", $date), status => "Added", newdate => "Y", suffix => date("Mj", $date));
            $gap = 0;
        } else {
            $gap++;
        }
        $date += 86400;
    }
    # derive a canonical string
    if ($d == 1) {
        # for invalid strings, use it unchanged
        $canonical = $str;
        $datestype = "error";
    } else {
        if ($d == 2) {
            # for single date, use a pretty form of that date
            $canonical = date("l, F j", $daylist[1]["timestamp"]);
            $datestype = "one";
        } else {
            if ($d > 2) {
                # for multiple days, check to see if consecutive.
                for ($i = 1; $i < $d - 1 && $daylist[$i]["timestamp"] > $daylist[$i + 1]["timestamp"] - 99999; $i++) {
                }
                if ($i == $d - 1) {
                    # Consecutive -- express it as a range of dates
                    $canonical = date("F j", $daylist[1]["timestamp"]) . " - ";
                    if (substr($daylist[2]["sqldate"], 1, 7) == substr($daylist[$d - 1]["sqldate"], 1, 7)) {
                        $canonical .= date("j", $daylist[$d - 1]["timestamp"]);
                    } else {
                        $canonical .= date("F j", $daylist[$d - 1]["timestamp"]);
                    }
                    $datestype = "consecutive";
                } else {
                    # Not consecutive -- decode the rule
                    $canonical = repeatcanonical($rules);
                    $datestype = "scattered";
                }
            }
        }
    }
    # Stuff the canonical string and datestype into the returned array too
    $daylist["canonical"] = $canonical;
    $daylist["datestype"] = $datestype;
    return $daylist;
}