hi there
wrote this a while back and had to change the date format but this should do what you want.
written before i played with timestamps etc so please dont tell me better ways of doing date calcs etc etc :-)
any questions please mail us - especially if you dont know how to call it properly.
cheers,
vince
bodev.com
<?
/*
simple php calendar
author: vincent fleetwood
to use this have a button in your main page which does a
window.open("calendar.php?control=" + nameOfTextbox + "&formname=" + nameOfForm)
where nameOfTextbox is the name of the textbox you would like the date put into
and nameOfForm is the forms name (on the main page)
if this doesnt make sense just mail me.
*/
// function to return days in any particluar month
function daysinmonth($m, $year)
{
$months = Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
// see if leap year
if (leapYear($year))
$months[1] = 29;
return $months[($m - 1)];
}
// whether the year passed is a leap year.
function leapYear ($year)
{
// returns true if leap year - i.e. year is divisible
// wholely by 400, or divisible by 4 but NOT by 100
return ((($year % 400) == 0) || ((($year % 4) == 0) && (($year % 100) != 0)));
}
function julian ($day, $month, $year)
{
// returns julian value of day and month
$jul = 0;
// array of months again
$months = Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
// see if leap year
if (leapYear($year))
{
$months[1] = 29;
}
// add days in month to work out days elapsed this year
for ($a = 0; $a < ($month - 1); $a++)
{
$jul += $months[a];
}
// add days elapsed this month
$jul += $day;
return jul;
}
function dateName ($day, $month, $year)
{
// returns day of week
// create array
$days = Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
$d = 0;
$Y = 0;
$newM = 0;
// convert year if jan or feb
if ($month < 3)
{
$year--;
}
// convert month to array format - 0 to 11, not 1 to 12
$month--;
// array of Zeller months
$zellMonth = Array(11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// convert month to Zeller month here
$month = $zellMonth[$month];
// multiply zellMonth * 2.6 and remove 2
$newM = (int) ((2.6 * $month) - 0.2);
$D = $day;
$Y = (int) ($year % 100);
// C is how many centuries
$C = (int) ($year / 100);
// need C over 4 as integer
$newC = (int) ($C / 4);
$yoverfour = (int)($Y / 4);
// zeller's congruence
$d = (int) (($newM + $D + $Y + ($yoverfour) + $newC - (2 * $C)) % 7);
// if negative needs additional 7
if ($d < 0)
{
$d = $d + 7;
}
return $days[($d)];
}
// returns day of week as int 0 - 6, sun - saturday
function dayofweek ($day, $month, $year)
{
// create array
$days = Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
$d = 0;
$Y = 0;
$newM = 0;
// convert year if jan or feb
if ($month < 3)
{
$year--;
}
// convert month to array format - 0 to 11, not 1 to 12
$month--;
// array of Zeller months
$zellMonth = Array(11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// convert month to Zeller month here
$month = $zellMonth[$month];
// multiply zellMonth * 2.6 and remove 2
$newM = (int) ((2.6 * $month) - 0.2);
$D = $day;
$Y = (int) ($year % 100);
// C is how many centuries
$C = (int) ($year / 100);
// need C over 4 as integer
$newC = (int) ($C / 4);
$yoverfour = (int)($Y / 4);
// zeller's congruence
$d = (int) (($newM + $D + $Y + ($yoverfour) + $newC - (2 * $C)) % 7);
// if negative needs additional 7
if ($d < 0)
{
$d = $d + 7;
}
return ($d);
}
// returns days between 2 dates
function daysElapsed ($day1, $month1, $year1, $day2, $month2, $year2)
{
$diff = 0;
$date1 = 0;
$date2 = 0;
// get days since 1st Jan 0AD (or something like that)
$date1 = daysSinceDayOne($day1, $month1, $year1);
$date2 = daysSinceDayOne($day2, $month2, $year2);
$diff = (int) ($date1 - $date2);
return $diff;
}
// speaks for itself
function daysSinceDayOne($day, $month, $year) {
$totalDays = 0;
$quads = 0;
$cents = 0;
$years = 0;
// veriable holding years passed
$y = ($year - 1);
$leapyears = 0;
// number of 400 years in y
$quads = (int) ($y / 400);
// means 97 leap years for each 400 years
$leapyears += ( 97 * $quads);
// unused centuries in year
$cents = (int)( ($y % 400) / 100);
// multiply by 24 for extra leapyears
$leapyears += (24 * $cents);
// then remainding years / 4 gives leap years this century
$leapyears += (int) (($y % 100) / 4);
return ( $leapyears + (365 * $y) + julian($day, $month, $year));
}
$ar = getDate();
$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
// see if passed the month to show
if (trim($monthindex) == "")
$reqdmonth = $ar['month'];
else
$reqdmonth = $months[$monthindex];
// see if passed the year to show
if (trim($year) == "")
$reqdyear = $ar['year'];
else
$reqdyear = $year;
function dotable($m, $y)
{
$months = array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");
// if passed month from prev year then roll back year
if ($m < 0)
{
$m+= 12;
$y--;
}
// and similar for month from next year
if ($m > 11)
{
$m-= 12;
$y++;
}
$mymon = strtoupper(substr($months[$m], 0, 3));
// convert to english form - i.e. 1 to 12, not 1 to 11.
$m++;
$myar = array(6, 0, 1, 2, 3, 4, 5);
// calculate day of week for first day of this month
$dn = (dayofweek(1, $m, $y));
$dn = $myar[$dn];
echo "<center>$mymon</center><table bgcolor='#0063a4'>";
echo "<tr>\n";
$lblar = array("M","T","W","Th","F","S","S");
for ($a = 0; $a < 7; $a++)
{
echo "<td align=center><font color=yellow><b>\n" . $lblar[$a] .
"</b></font></td>";
}
echo "</tr>";
echo "<tr>";
// starting bgcolor = silver but this will alternate
$bgcolor = 'silver';
// six possible weeks - cant explain why :-(
for ($a = 0; $a < (42); $a++)
{
echo "<td align=center bgcolor=$bgcolor> ";
if ($bgcolor == 'white')
{
$bgcolor = 'silver';
}
else
{
$bgcolor = 'white';
}
// this way of doing days is quite
// the long way round but worked for me :-/
if ($a >= $dn)
{
$ptr++;
if ($ptr < 10)
{
$dd = "0$ptr";
}
else
{
$dd = $ptr;
}
$mm = $m;
if($mm < 10)
{
$mm = "0$m";
}
if ($ptr <= daysinmonth($m, $y))
{
$mylink = "<a href='javascript: dodate(\"$y-$mm-$dd\");'>";
echo $mylink . $ptr;
}
}
echo "</td>";
// new line for each sunday
if ((($a + 1) % 7) == 0)
{
echo "</tr><tr>";
$bgcolor = 'silver';
}
}
echo "</tr>";
echo "</table>";
}
?>
<head>
<title>Simple PHP Calendar</title>
<style type='text/css'>
<!--
BODY
{
FONT-FAMILY: Verdana, Geneva, Arial, Helvetica, sans-serif;
FONT-SIZE: 9px
}
TD
{
FONT-FAMILY: Verdana, Geneva, Arial, Helvetica, sans-serif;
FONT-SIZE: 9px;
FONT-WEIGHT: normal;
}
A
{
COLOR: black;
TEXT-DECORATION: none
}
A:hover
{
COLOR: #cc0000;
BACKGROUND: yellow;
}
SELECT
{
COLOR: blue;
FONT: 9px verdana,tahoma,arial;
TEXT-DECORATION: none
}
//-->
</style>
<script>
<!-- hide from old browsers - although hot to use this without javascript i have no idea
function dodate(mydate)
{
parent.opener.document.forms[0].elements['<? echo $control; ?>'].value =
mydate;
parent.opener.document.forms[0].elements['<? echo $control; ?>'].focus();
window.close();
}
function changepage()
{
var ind = document.forms['myform'].elements['pageyear'].selectedIndex;
var year = document.forms['myform'].elements['pageyear'].options[ind].value;
var ind = document.forms['myform'].elements['pagemonth'].selectedIndex;
var pagename = "calendar.php?monthindex=" + ind + "&year=" + year + "&control=<?
echo $control; ?>&formname=<? echo $formname; ?>";
document.location.href = pagename;
}
//-->
</script>
</head>
<body>
<form name='myform'>
<table width='100%'>
<tr>
<td width='33%'></td>
<td width='33%' align=center>
<!-- table for showing month, year, etc. -->
<table>
<tr>
<td align=center>
<select name = 'pagemonth' onchange = 'changepage()'>
<?
for ($a = 0; $a < count($months); $a++)
{
if (strtolower($months[$a]) == strtolower($reqdmonth))
{
$issel = " selected";
$monthindex = $a;
}
else
{
unset($issel);
}
echo "\n<option value = '" . $a . "'$issel>" . $months[$a];
}
?>
</select>
</td>
</tr>
<tr>
<td align=center>
<select name='pageyear' onchange = 'changepage()'>
<?
$start = $reqdyear - 10;
$end = ($ar['year'] + 10);
for ($a = $start; $a < $end; $a++)
{
if ($a == (int)($reqdyear))
{
$issel = " selected";
}
else
{
unset($issel);
}
echo "<option value='$a'$issel>$a \n";
}
?>
</select>
</td>
</tr>
</table>
</td>
<td width='33%'></td>
</tr>
<!-- three tds, each with a table holding a month -->
<tr>
<td align=center>
<? dotable($monthindex - 1, $reqdyear); ?>
</td>
<td align=center>
<? dotable($monthindex, $reqdyear); ?>
</td>
<td align=center>
<? dotable($monthindex + 1, $reqdyear); ?>
</td>
</tr>
</table>
</form>
</body>