Well, why not just keep extending the
language support for this? Since this is a web-based world we live in, and as
PHP is more
hip than a pair of Wayfarers or anything bought at
Old Navy, I thought I'd convert this to a
web-based page (named, in this case, "dow.php") using PHP as the language. I'll post the
code first, then explain it...
<html>
<head>
<title>Day of week</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<body>
<form name="getDate" method="post" action="dow.php">
<table border="0" cellspacing="0" cellpadding="4">
<tr>
<td nowrap>
<font size="2" face="Arial, Helvetica, sans-serif">
Enter Date (mm/dd/yyyy):
</font>
</td>
<td nowrap>
<font size="2" face="Arial, Helvetica, sans-serif">
<input name="mon" type="text" size="2" maxlength="2">
/
<input name="day" type="text" size="2" maxlength="2">
/
<input name="year" type="text" size="4" maxlength="4">
</font>
</td>
<td nowrap>
<font size="2" face="Arial, Helvetica, sans-serif">
ex: "05/20/1960"
</font>
</td>
</tr>
<tr>
<td>
<font size="2" face="Arial, Helvetica, sans-serif"> </font>
</td>
<td>
<font size="2" face="Arial, Helvetica, sans-serif">
<input name="submit" type="submit" value="Submit Date">
</font>
</td>
<td>
<font size="2" face="Arial, Helvetica, sans-serif"> </font>
</td>
</tr>
</table>
</form>
<?php // begin php code
$fontStyle = "size=\"2\" face=\"Arial, Helvetica, sans-serif\"";
$dayStrings = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
if (!isset($mon) || !isset($day) || !isset($year))
return; // prevent processing of values not yet entered
// simple range-checking
if (($mon < 1) || ($mon > 12) ||
($day < 1) || ($day > 31) ||
($year < 0))
{
echo "<b>An invalid date ($mon/$day/$year) was entered </b>";
return;
}
if ($mon < 3)
{
$mon = $mon + 12;
$year = $year - 1;
}
if ($year < 1582) // account for gregorian calendar
$greg = 1;
else
$greg = 0;
$dow = ($day + (2 * $mon) + ((6 * ($mon + 1)) / 10) + $year + ($year / 4) -
($year / 100) + ($year / 400) + $greg) % 7;
echo "<font $fontStyle>"; // specify a font style
if ($dow > 6) { // trap out-of-range errors
echo "Something is amiss, the day ($dow) is invalid";
} else {
echo "The day of the week for $mon/$day/$year is:
<b>$dayStrings[$dow]</b>";
}
echo "</font>";
?>
</body>
</html>
I attempted to keep the code small, while still giving it some pizzazz so it works out-of-the-box and looks somewhat polished. The section enclosed by the PHP script identifiers ("<?php" and "?>") is a blatant rip off of the previous C code posted here, converted of course to PHP syntax. The remainder is just simple HTML code for data input, and display.
The above will display a basic HTML form where the user enters "the date" that interests them, and the page then returns which day of the week that date fell upon.
You can simply cut and paste the example into an editor, save it onto a PHP-enabled server (most are), and pull it up in your browser. If you change the saved filename to something other than "dow.php" you'll need to change the form code that links to itself by this name (I didn't use "PHP_SELF" because it's server configuration dependent).