...

View Full Version : Help with a PHP calendar script



galtmilemedia
07-23-2012, 05:54 PM
I am working on a PHP calendar script that will display a calendar, and my client will have a form so she can add events to the calendar.

So far the months of the year are showing up and the first row of the calendar is showing up. The year is not. Can anyone look at the code and offer some insights?

This is the link to what is showing now. http://www.skinsenseminimedspa.com/showcalendar.php

Below is the full code:



<?php
define("ADAY", (60*60*24));
if ((!isset($_POST['month'])) || (!isset($_POST['year'])))
{
$nowArray = getdate();
$month = $nowArray['mon'];
$year = $nowArray['year'];
}
else
{
$month = $_POST['month'];
$year = $_POST['year'];
}
$start = mktime (12,0,0,$month,1,$year);
$firstDayArray = getdate($start);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo "Calendar: " .$firstDayArray['month']." ".$firstDayArray['year']; ?></title>
<style type="text/css">
table {
border:1px solid black;
border-collapse:collapse;
}
th {
border:1px solid black;
padding:6px;
font-weight:bold;
background:#cccccc;
}
td {
border: 1px solid black;
padding:6px;
vertical-align:top;
width:100px;
}
</style>
</head>
<body>
<h1>Select a Month/Year Combination</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<select name="month">
<?php
$months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
for ($x=1; $x <= count($months); $x++)
{
echo "<option value=\"$x\"";
if ($x == $month)
{
echo " selected";
}
echo ">" .$months[$x-1]. "</option>";
}
?>
</select>
<select name="year">
<?php
for ($y=2012; $y<=2020; $y++)
{
echo "<option value=\"$y\"";
if ($y == $year)
{
echo " selected";
}
else ">" .$y. "</option>";
}
?>
</select>

<button type="submit" name="submit" value="submit">Go!</button><br />

</form>
<br />
<?php
$days = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
echo "<table><tr>\n";
foreach ($days as $day)
{
echo "<td>" .$day."</td>\n";
}
for ($count=0; $count < (6*7); $count++)
{
$dayArray = getdate($start);
if (($count % 7) == 0)
{
if ($dayArray['mon'] != $month)
{
break;
}
else
{
echo "</tr><tr>\n";
}
}
if (count < $firstDayArray['wday'] || $dayArray['mon'] !=$month)
{
echo "<td>".$dayArray['mday']."</td>\n";
$start += ADAY;
}
}
echo "</tr></table>";
?>
</body>
</html>

Keleth
07-23-2012, 06:04 PM
You mean the years in the dropdown?

galtmilemedia
07-23-2012, 06:05 PM
yes and the go button is supposed to show up. Below that is a full month long calendar.

Arcticwarrio
07-24-2012, 01:13 PM
its working ok in firefox

Arcticwarrio
07-24-2012, 04:50 PM
ah you mean the bit you posted,

got it working sort of


here:


<?php
define("ADAY", (60*60*24));
if ((!isset($_POST['month'])) || (!isset($_POST['year'])))
{
$nowArray = getdate();
$month = $nowArray['mon'];
$year = $nowArray['year'];
}
else
{
$month = $_POST['month'];
$year = $_POST['year'];
}
$start = mktime (12,0,0,$month,1,$year);
$firstDayArray = getdate($start);
print_r($firstDayArray);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo "Calendar: " .$firstDayArray['month']." ".$firstDayArray['year']; ?></title>
<style type="text/css">
table {
border:1px solid black;
border-collapse:collapse;
}
th {
border:1px solid black;
padding:6px;
font-weight:bold;
background:#cccccc;
}
td {
border: 1px solid black;
padding:6px;
vertical-align:top;
width:100px;
}
</style>
</head>
<body>
<h1>Select a Month/Year Combination</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<select name="month">
<?php
$months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
for ($x=1; $x <= count($months); $x++)
{
echo "<option value=\"$x\"";
if ($x == $month)
{
echo " selected";
}
echo ">" .$months[$x-1]. "</option>";
}
?>
</select>
<select name="year">
<?php
for ($y=2012; $y<=2020; $y++)
{
echo "<option value=\"$y\"";
if ($y == $year)
{
echo " selected";
}
echo ">" .$y. "</option>";
}
?>
</select>

<button type="submit" name="submit" value="submit">Go!</button><br />

</form>
<br />
<?php
$days = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
echo "
<table><tr>";
foreach ($days as $day){
echo "
<td>" .$day."</td>";
}
if ($firstDayArray['wday'] == 0){$CDays = 35;}else{$CDays = 42;}
for ($count=0; $count < $CDays; $count++){
$dayArray = getdate($start);
if ($dayArray['mon'] == $month){
if (($count % 7) == 0){echo "
</tr><tr>";}
if ($count < $firstDayArray['wday']) {
echo "
<td></td>";
}else{
echo "
<td>".$dayArray['mday']."</td>";
$start += ADAY;
}
}
}
echo "
</tr></table>";
?>
</body>
</html>

Arcticwarrio
07-24-2012, 07:59 PM
Fixed it,

re-wrote the way it looks for the month


<?php
define("ADAY", (60*60*24));
if ((!isset($_POST['month'])) || (!isset($_POST['year'])))
{
$nowArray = getdate();
$month = $nowArray['mon'];
$year = $nowArray['year'];
}
else
{
$month = $_POST['month'];
$year = $_POST['year'];
}
$start = mktime (12,0,0,$month,1,$year);
$firstDayArray = getdate($start);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo "Calendar: " .$firstDayArray['month']." ".$firstDayArray['year']; ?></title>
<style type="text/css">
table {
border:1px solid black;
border-collapse:collapse;
}
th {
border:1px solid black;
padding:6px;
font-weight:bold;
background:#cccccc;
}
td {
border: 1px solid black;
padding:6px;
vertical-align:top;
width:100px;
}
</style>
</head>
<body>
<h1>Select a Month/Year Combination</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<select name="month">
<?php
$months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
for ($x=1; $x <= count($months); $x++){
echo "<option value=\"$x\"";
if ($x == $month){
echo " selected";
}
echo ">" .$months[$x-1]. "</option>";
}
?>
</select>
<select name="year">
<?php
for ($y=2012; $y<=2020; $y++){
echo "<option value=\"$y\"";
if ($y == $year){
echo " selected";
}
echo ">" .$y. "</option>";
}
?>
</select>

<button type="submit" name="submit" value="submit">Go!</button><br />

</form>
<br />
<?php
$days = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
echo "
<table><tr>";
foreach ($days as $day){
echo "
<td>" .$day."</td>";
}

for ($count=0; $count < 42; $count++){
$dayArray = getdate($start);
if ($dayArray['mon'] == $month){
if (($count % 7) == 0){echo "
</tr><tr>";}
if ($count < $firstDayArray['wday']) {
echo "
<td></td>";
}else{
echo "
<td>".$dayArray['mday']."</td>";
$start += ADAY;
}
}else{
if (($count % 7) == 0){
exit;
}
else{
echo "
<td></td>";
}
}
}
echo "
</tr></table>";
?>
</body>
</html>

DrDOS
07-24-2012, 09:02 PM
It's starting to look really nice. I think you're going to have a winner.

Nicodemus
07-24-2012, 09:50 PM
It looks good. How are you getting along with the form to enter the events? I presume that you'll store the data in an SQL database and have the calendar interrogate it.

I hope you'll feel willing to share the final code when you've completed it

Arcticwarrio
07-24-2012, 10:11 PM
i like this calender so much, i going to use it for our company holidays,

i'll use the raw date as the sql key $dayArray['0'] then add links to a Day View page something like:


<?php
define("ADAY", (60*60*24));
if ((!isset($_POST['month'])) || (!isset($_POST['year'])))
{
$nowArray = getdate();
$month = $nowArray['mon'];
$year = $nowArray['year'];
}
else
{
$month = $_POST['month'];
$year = $_POST['year'];
}
$start = mktime (12,0,0,$month,1,$year);
$firstDayArray = getdate($start);
print_r($firstDayArray);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo "Calendar: " .$firstDayArray['month']." ".$firstDayArray['year']; ?></title>
<style type="text/css">
table {
border:1px solid black;
border-collapse:collapse;
}
th {
border:1px solid black;
padding:6px;
font-weight:bold;
background:#cccccc;
}
td {
border: 1px solid black;
padding:6px;
vertical-align:top;
width:100px;
}
</style>
</head>
<body>
<h1>Select a Month/Year Combination</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<select name="month">
<?php
$months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
for ($x=1; $x <= count($months); $x++){
echo "<option value=\"$x\"";
if ($x == $month){
echo " selected";
}
echo ">" .$months[$x-1]. "</option>";
}
?>
</select>
<select name="year">
<?php
for ($y=2012; $y<=2020; $y++){
echo "<option value=\"$y\"";
if ($y == $year){
echo " selected";
}
echo ">" .$y. "</option>";
}
?>
</select>

<button type="submit" name="submit" value="submit">Go!</button><br />

</form>
<br />
<?php
$days = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
echo "
<table><tr><th colspan=\"7\">".$firstDayArray['month']." - ".$firstDayArray['year']."</th></tr>";
foreach ($days as $day){
echo "
<th>" .$day."</th>";
}

for ($count=0; $count < 42; $count++){
$dayArray = getdate($start);
if ($dayArray['mon'] == $month){
if (($count % 7) == 0){echo "
</tr><tr>";}
if ($count < $firstDayArray['wday']) {
echo "
<td bgcolor=\"#999999\"></td>";
}else{
echo "
<td><a href=\"calender.php?date=".$dayArray['0']."\">".$dayArray['mday']."</a></td>";
$start += ADAY;
}
}else{
if (($count % 7) == 0){
exit;
}
else{
echo "
<td bgcolor=\"#999999\"></td>";
}
}
}
echo "
</tr></table>";
?>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum