...

View Full Version : php calendar help please



LJackson
10-15-2009, 03:33 PM
I have created a basic calendar


but the width of the container stretches across the screen for some reason but i cannot see why? any ideas?

here is my code

<!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>Untitled Document</title>
<style type="text/css">
.calendarcontainer{
float:left;
border:2px solid #000000;
background-color:#000000;
}
.calendarHeader{
float:left;
width:500px;
font-family:"Hobo Std", "Arial Rounded MT Bold";
font-size:18px;
}
.calendarDaysHeader{
float:left;
width:110px;
height:30px;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:14px;
margin:1px;
font-weight:bold;
text-align:center;
}
.dayHeaders{
float:left;
width:800px;
background-color:#9999CC;
}
.weekcontainer{
float:left;
width:800px;
height:80px;
background-color:#FF0000;
}
.day{
float:left;
width:110px;
height:80px;
background-color:#CCCC66;
font-family:"Hobo Std", "Arial Rounded MT Bold";
font-size:18px;
margin:1px;
}
</style>
</head>

<body>
<?php
//This gets today's date
$date =time () ;

//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = date('m', $date) ;
$year = date('Y', $date) ;

//Here we generate the first day of the month

$first_day = mktime(0,0,0,$month, 1, $year) ;

//This gets us the month name
$title = date('F', $first_day) ;

//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;

//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}

//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year);

//Here we start building the table heads
?>
<div class="calendarcontainer">
<div class="calendarMonth"><?php print $title.$year?></div>
<div class="dayHeaders">
<div class="calendarDaysHeader">Sunday</div>
<div class="calendarDaysHeader">Monday</div>
<div class="calendarDaysHeader">Tuesday</div>
<div class="calendarDaysHeader">Wednesday</div>
<div class="calendarDaysHeader">Thursday</div>
<div class="calendarDaysHeader">Friday</div>
<div class="calendarDaysHeader">Saturday</div>
</div><?php
//This counts the days in the week, up to 7
$day_count = 1;?>

<div class="weekcontainer"><?php
//first we take care of those blank days
while ( $blank > 0 )
{
?>
<div class="day"></div><?php
$blank = $blank-1;
$day_count++;
}

//sets the first day of the month to 1
$day_num = 1;

//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
?>
<div class="day"><?php print $day_num?></div>
<?php
$day_num++;
$day_count++;

//Make sure we start a new row every week
if ($day_count > 7)
{
?>
</div>
<div class="weekcontainer"><?php
$day_count = 1;
}
}

//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 )
{
?>
<div class="day"></div><?php
$day_count++;
}
?>

</div></div>

</body>
</html>


any help appreciated
thanks
Luke

Fumigator
10-15-2009, 05:11 PM
You didn't give it a width so it's going to use everything available to it.

LJackson
10-15-2009, 05:28 PM
ah ok thanks mate thought that without a width it would just be as wide as the contents inside it :)

cheers
Luke

LJackson
10-15-2009, 06:58 PM
ok i require some help please with adding a previous and next button which will increase the value of month either by one or decrease month by 1

i have set up the buttons

<form id="form1" name="form1" method="post" action="calendar.php?<?php echo $months?>">
<input type="submit" name="previous" id="previous" value="&lt;--" />
<?php print $title." ".$year?>
<input type="submit" name="next" id="next" value="--&gt;" />
</form>


but when i click on one of them it reloads the page and displays the value of $months which is ok, but you hav to press the button twice for it to change the value of $months :(

i have this at the top of my page

if(isset($_POST['previous'])){
$months = "- 1";
}
if(isset($_POST['next'])){
$months = "+ 1";
}

what i intend to do is if next is pressed then i will add the 1 to another value to get the month value to determin which month to display.

not sure if this seems the best way or not??

any ideas as to why it is taking two clicks to change the post value?

thanks
Luke

Fumigator
10-15-2009, 07:35 PM
I would use the DateTime object and its "add" method to add a month.

http://us.php.net/manual/en/datetime.add.php

LJackson
10-15-2009, 09:31 PM
hi mate for some reason i could not get that to work it said add_date is unrecognised or something to that extent :(

i have managed to get it kinda working, but the only problems im having is if you click to go to the next month you have to click twice for it to change then if you want to go back, first time you click back it goes forward then if you click back again it goes back so it seems to be taking two clicks to change any ideas please!!

this is what i have so far

<?php
include_once("db_info.php");?>
<!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>Untitled Document</title>
<style type="text/css">
.calendarcontainer{
float:left;
width:800px;
border:2px solid #000000;
}
.calendarHeader{
float:left;
width:500px;
font-family:"Hobo Std", "Arial Rounded MT Bold";
font-size:18px;
}
.calendarDaysHeader{
float:left;
width:110px;
height:30px;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:14px;
margin:1px;
font-weight:bold;
border:1px solid #000000;
}
.dayHeaders{
float:left;
width:800px;
}
.weekcontainer{
float:left;
width:800px;
height:90px;
margin-bottom:2px;
}
.day{
float:left;
width:110px;
height:90px;
margin:1px;
border:1px solid #000000;
}
.displayDay{
float:left;
width:110px;
height:25px;
font-family:"Hobo Std", "Arial Rounded MT Bold";
font-size:16px;
}
.displayEvent{
float:left;
width:110px;
height:20px;
background-color:#CC33FF;
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
}
</style>
</head>

<body>
<?php
function SimpleCalendar($year, $month, $day)
{
//This gets today's date
$date =time () ;

//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
if(isset($_POST['currentMonth'])){
$month = $_POST["currentMonth"];
}else
{
$month = date('m', $date) ;
}

if(isset($_POST['currentYear'])){
$year = $_POST["currentYear"];
}else
{
$year = date('Y', $date) ;
}

//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;

//This gets us the month name
$title = date('F', $first_day) ;

//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;

//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}

//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year);

//Here we start building the table heads
?>
<div class="calendarcontainer">
<div class="calendarMonth"><?php print $title." ".$year?></div>
<div class="dayHeaders">
<div class="calendarDaysHeader">Sunday</div>
<div class="calendarDaysHeader">Monday</div>
<div class="calendarDaysHeader">Tuesday</div>
<div class="calendarDaysHeader">Wednesday</div>
<div class="calendarDaysHeader">Thursday</div>
<div class="calendarDaysHeader">Friday</div>
<div class="calendarDaysHeader">Saturday</div>
</div><?php
//This counts the days in the week, up to 7
$day_count = 1;?>

<div class="weekcontainer"><?php
//first we take care of those blank days
while ( $blank > 0 )
{
?>
<div class="day"></div><?php
$blank = $blank-1;
$day_count++;
}

//sets the first day of the month to 1
$day_num = 1;

//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
?>
<div class="day">
<div class="displayDay"><?php print $day_num?></div>
<?php
//CREATE TEMP NEW DATE
$newday = "$year-$month-$day_num";
//print $newday;
//CHECK DB FOR EVENT
$check = "SELECT * FROM events WHERE DATE(event_date) = '$newday'";
$sql = mysql_query($check)or die(mysql_error());
while ($event = mysql_fetch_array($sql)){?>
<div class="displayEvent"><?php print $event['headline']?></div>
<?php } ?>
</div>
<?php
$day_num++;
$day_count++;

//Make sure we start a new row every week
if ($day_count > 7 && $day_num <= $days_in_month)
{
?>
</div>
<div class="weekcontainer"><?php
$day_count = 1;
}
}

//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7)
{
?>
<div class="day"></div><?php
$day_count++;
}

}

if(isset($_POST["back"]))
{
$month = $_POST["currentMonth"];
$year = $_POST["currentYear"];
$day = $_POST["currentDay"];
if($month == 1)
{
$month = 12;
$year = $year-1;
}
else
{
$month=$month-1;
}
}

elseif (isset($_POST["next"]))
{
$month = $_POST["currentMonth"];
$year = $_POST["currentYear"];
$day = $_POST["currentDay"];

if($month == 12)
{
$month = 1;
$year = $year+1;
}
else
{
$month=$month+1;
}
}
else
{
// use the current date
$date =time () ;
$day = date('d', $date) ;
$month = date('m', $date) ;
$year = date('Y', $date) ;
}

// call the calendar code
SimpleCalendar($year, $month, $day);
print $day." ".$month." ".$year;?>

<form id="form1" name="form1" method="post" action="calendar.php">
<input type="submit" name="back" id="back" value="&lt;--" />
<input type="submit" name="next" id="next" value="--&gt;" />
<input type="hidden" name="currentMonth" id="currentMonth" value="<?php echo $month?>" />
<input type="hidden" name="currentYear" id="currentYear" value="<?php echo $year?>" />
<input type="hidden" name="currentDay" id="currentDay" value="<?php echo $day?>" />

</form>

</div>
</div>

</body>
</html>

any ideas where im going wrong!

thanks
Luke

LJackson
10-16-2009, 02:48 PM
all sorted now thanks :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum