Jump to content

calender problems


aleksanteri
 Share

Recommended Posts

OK, I'm doing a calendar but I don't know what is wrong on making PHP clear where is the month's first day. My solution was to valiadate each date's week number (0=sunday, 6=saturday).I don't know what is now wrong... it starts from saturday.

<?php$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");$daynames = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");$timestamp = time();$month = (isset($_GET["month"])) ? $_GET["month"] : date("F",$timestamp);$year = (integer) (isset($_GET["year"])) ? $_GET["year"] : date("Y",$timestamp) ;$monthname = $month;foreach($months as $index => $temp){	if($temp == $month)		$month = $index;}$timestamp = mktime(0, 0, 0, $month, $year);$days = (integer) date("t", $timestamp);$monthnum = (integer) date("n", $timestamp);?><html><body><form action="date.php" method="GET"><!-- month selector --><select name="month"><?phpforeach($months as $x){	$sel = "";	if($x == $month)		$sel = " selected=\"selected\"";	print("\t<option value=\"$x\" $sel>$x</option>\n");}?></select><!-- year selector --><select name="year"><?phpfor($y = 1970; $y <= 2010; $y++){	$totals++;	$sel = "";	if($y == $year)		$sel = " selected=\"selected\"";	print("\t<option value=\"$y\" $sel>$y</option>\n");}?></select><input type="submit" /></form><table border="1" cellspacing="0" style="text-align: center"><tr><?phpfunction validate_month($weekday_int, $month, $year, $date){	$info_timestamp = mktime(0, 0, 0, $month, $date, $year);		$right_week = date("w", $info_timestamp);		if($weekday_int == $right_week)		return true;	else		return false;}foreach($daynames as $day)	print("\t<th>$day</th>\n");?></tr><?php$cellnum = 0;$datecount = 1;$weekdaycount = 1;for($x = 1; $x <= $days; ++$x){	$cellnum++;	if($datecount % 7 == 1 && $datecount != 1)		print("</tr>\n<tr>\n");	if(validate_month($weekdaycount, $month, $year, $x))	{		print("\t<td>$monthname $x</td>\n");		++$weekdaycount;	}	else	{		print("\t<td> </td>\n");	}	++$datecount;}?></table></body></html>

thanks for advice.

Link to comment
Share on other sites

One thing I noticed is you left the day out of mktime. PHP makes it pretty easy to get the month name and day name.

$now = getdate();$month = (isset($_GET['month']) ? intval($_GET['month']) : $now['mon']);$day = (isset($_GET['day']) ? intval($_GET['day']) : $now['mday']);$year = (isset($_GET['year']) ? intval($_GET['year']) : $now['year']);$dateobj = getdate(mktime(0, 0, 0, $month, $day, $year));$monthname = $dateobj['month'];$dayname = $dateobj['weekday'];$daynum = $dateobj['wday']; // 0 = sunday, 6 = saturday$first_day = getdate(mktime(0, 0, 0, $month, 1, $year));

That gives you the current day and the first day of the current month. If you are making a calendar, you will probably want to go back however many days it takes into the previous month to start at sunday or whatever, and grey those days out until the start of the month when you start writing the day numbers (which is 'mday' in the getdate array). And then when you get to the last day of the month, you will want to advance until saturday and grey those days out also so that you end up with an even rectangle for the month.I'm not sure what you mean with your problem though where it starts from saturday.

Link to comment
Share on other sites

I mean I try to stop the month starting from sunday, you know to make the weekday valid for each date, the empty spaces before "1 June".However, although June 2006 starts from thursday, 1 June on my calendar starts on saturday...I used mktime to update the $timestamp with the new info.I am not able to use getdate(), it returns an assosiative array and my PHP shows me a warning of having constants when I try to use it.

$getdate = getdate(time());$getdate[mon]

returns a warning that constant "mon" is undefined...

Link to comment
Share on other sites

You need to quote the key.$getdate['mon'];When you have the first day of the month, you can use a for loop to draw everything up until then. To add on to my earlier example:

$now = getdate();$month = (isset($_GET['month']) ? intval($_GET['month']) : $now['mon']);$day = (isset($_GET['day']) ? intval($_GET['day']) : $now['mday']);$year = (isset($_GET['year']) ? intval($_GET['year']) : $now['year']);$dateobj = getdate(mktime(0, 0, 0, $month, $day, $year));$monthname = $dateobj['month'];$dayname = $dateobj['weekday'];$daynum = $dateobj['wday']; // 0 = sunday, 6 = saturday$first_day = getdate(mktime(0, 0, 0, $month, 1, $year));for ($i = 0; $i < $first_day['wday']; $i++){  //draw an empty day}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...