Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

showing date range in php calender

Status
Not open for further replies.

NickyJay

Programmer
Sep 1, 2003
217
GB
Hi All,
i have a calendar app on my site that displays a users diary of events, i've hit a problem on how to get this to show a block of dates with the same event - holiday for two weeks, the user would have to add this event to each date individually instead of just a from and to date. I have adjust my mysql database now to include an end date for an event (defaulting to the dame start date for 1 day events), but how do i show the start and end dates?

Here is my code to retrieve the info - please note that i am a beginner and this is a script i have found and modified but it has taken ages to get my head round!
Code:
<?php
<?php
include ('./connections.php');

// Now we need to define "A DAY", which will be used later in the script:
define("ADAY", (60*60*24));

// The rest of the script will stay the same until about line 82
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);
?>
<html>
<head>
<title><?php echo "Craig's Schedule: ".$firstDayArray['month']." " . $firstDayArray['year']; ?></title>
	<link rel="stylesheet" href="../css/default.css" type="text/css">
    <link rel="stylesheet" href="../css/admin.css" type="text/css">
</head>
<script type="text/javascript">
	function eventWindow(url) {
		event_popupWin = window.open(url, 'event', 'resizable=yes,scrollbars=yes,toolbar=no,width=500,height=500');
		event_popupWin.opener = self;
	}
</script>

<body>

	<div id="container">
    <div id="content">
    <div id="content_leaves">
		  
		<div id="calendar">
		<h1>Select a Month/Year</h1>
		<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
			<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 ($x=1980; $x<=2010; $x++){
						echo "<option";
						if ($x == $year){
								echo " selected";
						}
						echo ">$x</option>";
				}
				?>
			</select>
			<input type="submit" name="submit" value="Go!">
		</form>
		
		<p>&nbsp;</p>
			<?php
			$days = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
			echo "<table border=\"0\" cellpadding=\"3\">\n";
			foreach ($days as $day) {
				echo "<th><strong>$day</strong>\n";
			}
			
			for ($count=0; $count < (6*7); $count++) {
					$dayArray = getdate($start);
					if (($count % 7) == 0) {
							if ($dayArray["mon"] != $month) {
									break;
							} else {
									echo "</th>
									<tr>\n";
							}
					}
					if ($count < $firstDayArray["wday"] || $dayArray["mon"] != $month) {
							echo "<td> </td>\n";
					} else {
							$chkEvent_sql = "SELECT EventTitle, EventVenue FROM $tbl_name1 WHERE month(EventDate) = '".$month."' AND dayofmonth(EventDate) = '".$dayArray["mday"]."' AND year(EventDate) = '".$year."' ORDER BY EventDate";
							$chkEvent_res = mysql_query($chkEvent_sql, $mysql) or die(mysql_error($mysql));
			
							if (mysql_num_rows($chkEvent_res) > 0) {
									$event = "<br />"; 
									while ($ev = mysql_fetch_array($chkEvent_res)) {
											$event .= "<div class=\"calendar_event\">".stripslashes($ev["EventTitle"])."</div> <div class=\"calendar_venue\">".stripslashes($ev["EventVenue"])."</div>";
									}
									mysql_free_result($chkEvent_res);
							} else {
									$event = "&nbsp;";
							}
			
							echo "<td valign=\"top\"><a href=\"javascript:eventWindow('./event.php?m=".$month."&d=".$dayArray["mday"]."&y=$year');\" class=\"calendar\">".$dayArray["mday"]."</a><br/>".$event."</td>\n";
			
							unset($event);
			
							$start += ADAY;
					}
			}
			echo "</tr>
				</table>";
			mysql_close($mysql);
			?>
		</div> <!-- calendar --> 
		  
     </div><!-- content leaves -->
    
    <div id="footer">
        	<? //include('includes/footer.php'); ?>
    </div> 
    
    </div><!-- content -->
    </div><!-- container -->

</body>
</html>
Many thanks for reading this - i hope someone can help!
 
are you sure that a start date and end date are the right approach? i can't help thinking that a table for the event (not date specific) and a join table for the days would be a better solution. if the event were multi-day you would include an entry in the day table for each day on which the event was active.

there are probably other ways to skin the proverbial cat.
 
Hiya,

to be honest im not sure what the right approach is! i'm new to php - only a couple of months coding - bu thave used coldfusion before, but i havent come across this scenario before. Any suggestions are welcome! Can you give me a bit of an example to work with? i am ok with the db side of it (am a dba dabbling in web!) but the php side is still foriegn to me!
Many thanks
 
it's not a php thing but a question of structural approach.

it's difficult to give an example without writing the whole code. if i have a spare half hour i will try to dream something up.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top