Working with a calendar

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dave_c00
Forum Commoner
Posts: 37
Joined: Wed May 28, 2003 6:08 am

Working with a calendar

Post by dave_c00 »

ImageBech100 | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Hello,

I currently have the code below that displays a calendar for a given month and year. I would like, however, to display the next 14 days from todays date so whnever the user clicks the current month or the next month they can see that two weeks from todays date is bold.

I have tried various functions but keep getting errors

Code: Select all

<?php
		define("ADAY", (60*60*24));
		if (!checkdate($month,1,$year)){
		$nowArray = getdate();
		$Tday = $nowArray['mday'];
		$month = $nowArray['mon'];
		$year = $nowArray['year'];
		}
		$start = mktime(12,0,0,$month,1,$year);
		$firstDayArray = getdate($start);
		?>
		<html>
		<head>
		<title><?php print "Calendar: ".$firstDayArray['month']
				." ".$firstDayArray['year']?></title>
		</head>
		<body>
		<form 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++){
			print "\t<option value="$x"";
			print ($x == $month)?" SELECTED":"";
			print ">".$months[$x-1]."\n";
		}
		?>
		</select>
		<select name="year">
		<?php
		for ($x=1980;$x<2010;$x++){
			print "\t<option";
			print ($x == $year)?" SELECTED":"";
			print ">$x\n";
		}
		?>
		</select>
		<input type="submit" value="Go!">
		</form>
		<p>
		<?php
		$days = Array("Sunday", "Monday", "Tuesday", "Wednesday",
						"Thursday", "Friday", "Saturday");
		print "<TABLE BORDER=1 CELLPADDING=0>\n";
		foreach ($days as $day)
			print "\t<td><b>$day</b></td>\n";
			for ($count=0;$count<(6*7);$count++){
			$dayArray = getdate($start);
			if ((($count)%7)==0){
			if($dayArray['mon']!=$month)
			break;
			print "</tr><tr>\n";
			}
			if ($count<$firstDayArray['wday']||$dayArray['mon']!=$month)
			print "\t<td><br></td>\n";
			
			else{
			print "\t<td>".$dayArray['mday']." ".$dayArray['month']."</td>\n";
			$start += ADAY;
			}
			}
			print "</tr></table>";
			?>
Thanks in advance

Dave.

ImageBech100 | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

it would be tough to do that unless you check the currently selected days timestamp with the timestamp of all the days as they are printed... let me see if i can dig up the calander i wrote, and you can look at it.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

i found it and added in some comments, mabe you can use it =)

Code: Select all

<?php  //  FILENAME:  calander.php
if(!empty($_GET)){extract($_GET);}elseif(!empty($HTTP_GET_VARS)){extract($HTTP_GET_VARS);}
if(!empty($_POST)){extract($_POST);}elseif(!empty($HTTP_POST_VARS)){extract($HTTP_POST_VARS);}
?>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link rel="stylesheet" type="text/css" href="calander.css">
<title>calander</title>
</head>

<body>
<table cellpadding="2" cellspacing="5" width="100%">
	<?php
	$current_day = 1;
	if(!$this_month){
		$this_month = date("n");
		$this_year = date("Y");
		$start_timestamp = mktime(0, 0, 0, $this_month, 1, $this_year);
	} else {
		$start_timestamp = mktime(0, 0, 0, $this_month, 1, $this_year);
	}
	
	if(($this_month + 1) == 13) { $next_month = 1; $next_year = $this_year + 1; }
	else { $next_month = $this_month + 1; $next_year = $this_year; }
	if(($this_month - 1) == 0) { $prev_month = 12; $prev_year = $this_year - 1; }
	else { $prev_month = $this_month - 1; $prev_year = $this_year; }
	
	$days_this_month = date("t", $start_timestamp);
	?>
	<tr>
		<td width="100%" valign="top" style="padding: 0px;">
			<!--  CALANDER  -->
			<table class="text_normal" border="1" cellpadding="2" cellspacing="0" style="border-collapse: collapse" bordercolor="#0033CC" width="100%" id="AutoNumber1">
				<tr>
					<td class="sub_title" width="100%" colspan="7"><a href="calander.php?this_month=<?php print $prev_month; ?>&this_year=<?php print $prev_year; ?>"><font color="#FFFFFF"><<</font></a> <?php print date("F, Y", $start_timestamp); ?> <a href="calander.php?this_month=<?php print $next_month; ?>&this_year=<?php print $next_year; ?>"><font color="#FFFFFF">>></font></a></td>
				</tr>
				<tr bgcolor="#CCCCCC">
					<td width="14%" valign="top" align="center">Sun</td>
					<td width="14%" valign="top" align="center">Mon</td>
					<td width="14%" valign="top" align="center">Tue</td>
					<td width="14%" valign="top" align="center">Wed</td>
					<td width="14%" valign="top" align="center">Thur</td>
					<td width="15%" valign="top" align="center">Fri</td>
					<td width="15%" valign="top" align="center">Sat</td>
				</tr><form action="calander.php?this_month=<?php print $this_month; ?>&this_year=<?php print $this_year; ?>" method="POST">
				<?php
				$weekday_start = date("w", $start_timestamp);
				for($h = 0; $h < 5; $h++){
					print"\t\t\t\t<tr>\n";
					for($i = 0; $i < 7; $i++){
						if($weekday_start == $i) { $start_calander = 1; }
						if($current_day > $days_this_month) { $start_calander = 0; }
						if($start_calander == 1){
							//  Get timestamp for today day, and for the current day that is printing.  //
							$current_timestamp = mktime(0, 0, 0, $this_month, $current_day, $this_year);
							$now_timestamp = mktime(0, 0, 0, date("n"), date("j"), date("Y"));
							
							//  Check to see if currently printing day is what im looking for.  //
							if($current_timestamp  == $now_timestamp) { $bgcolor="bgcolor="#CCCCCC" "; }
							elseif(($current_timestamp <= $now_timestamp + 604800) && ($current_timestamp >= $now_timestamp - 604800)) { $bgcolor="bgcolor="#DDDDDD" "; }
							else { $bgcolor = ""; }

							print"\t\t\t\t\t<td $bgcolor width="14%" height="75" valign="top">$current_day<br>\n";
							print"</td>\n";
							$current_day++;
						} else {
							print"\t\t\t\t\t<td width="14%" height="75" valign="top">&nbsp;</td>\n";
						}
					}
					print"\t\t\t\t</tr>\n";
				}
				?>
			</form></table>
		<!--  END CALANDER -->		
		</td>
	</tr>	
	<tr>
		<td class="sub_title" width="100%" align="right">
		<!--  FOOTER  -->
		<?php print date("M d, Y"); ?>
		<!--  END FOOTER  -->
		</td>
	</tr>
</table>

</body>
</html>
and here is the CSS for it (filename: calander.css)

Code: Select all

&lt;style&gt;
a &#123; text-decoration: none; color: #0033CC;&#125;
a:link &#123; text-decoration: none; color: #0033CC;&#125;
a:visited &#123; text-decoration: none; color: #0033CC;&#125;
a:hover &#123; text-decoration: underline; color: #0033CC;&#125;
a:active &#123; text-decoration: none; color: #0033CC; &#125;

body
&#123;
	background-image: url('images/bg_01.png');
	background-repeat: no-repeat;
	background-position: center top;
&#125;

.menu
&#123;
	border:1px solid #0033CC;
	background-image: url('images/title_01.gif');
	background-repeat: no-repeat;
	background-position: left top;
	font:12px Arial;
	font-weight: Bold;
	color: #0033CC;
&#125;

.sub_title
&#123;
	border: 1px solid #0033CC;
	background-color: #0033CC;
	font: 12px Arial;
	font-weight: Bold;
	color: #FFFFFF;
&#125;

.text_bold_border
&#123;
	border: 1px solid #0033CC;
	font:12px Arial;
	font-weight: Bold;
	color: #0033CC;
&#125;

.text_normal_border
&#123;
	border: 1px #0033CC solid;
	font:12px Arial;
	font-weight: Normal;
	color: #0033CC;
&#125;

.text_normal
&#123;
	font:12px Arial;
	font-weight: Normal;
	color: #0033CC;
&#125;

.news_header
&#123;
	font:18px Arial;
	font-weight: Bold;
	color: #0033CC;
&#125;
.news_date
&#123;
	font:10px Arial;
	font-weight: Bold;
	color: #0033CC;
&#125;

.input_button
&#123;
	border: 1px #0033CC solid;
	background-color: #FFFFFF;
	font:12px Arial;
	font-weight: bold;
	color: #0033CC;
	width: 100px;
&#125;
&lt;/style&gt;
dave_c00
Forum Commoner
Posts: 37
Joined: Wed May 28, 2003 6:08 am

Post by dave_c00 »

That looks really good. Thanks...
Post Reply