Jump to content

Something Is Really Wrong


ckrudelux
 Share

Recommended Posts

Okay so I made this small code for a calendar showing which date you are on and what dates have past and coming up but something has gone really wrong.. Then I'm showing a past month and selected a date I get all dates selected form that point and forward.Here is the script:

<head>	<title>		Blog	</title>	<link rel="stylesheet" href="blog.css" type="text/css"></head><body><div id="calender"><?phpif(isset($_GET['y']) and $_GET['y'] != ""){	$year = $_GET['y'];}else{	$year = date("Y");}if(isset($_GET['m']) and $_GET['m'] != ""){	$month = $_GET['m'];}else{	$month = date("m");}if(isset($_GET['d']) and $_GET['d'] != ""){	$selected = $_GET['d'];}else{	$selected = date("d");}$day = date("d");$day--;$numberofdays = cal_days_in_month(CAL_GREGORIAN, $month, $year);$countingdays = 0;while($numberofdays != $countingdays){		if($countingdays == $day and date("Ym") == $year.$month){		$class = "today";	}else if($countingdays < $day and date("Ym") >= $year.$month){		$class = "pastdays";	}else if(date("Ym") <= $year.$month){		$class = "comingdays";	}		$countingdays++;	if($countingdays == $selected){		$class = $class." selected";	}	echo "<a class=\"".$class."\" href=\"?y=".$year."&m=".$month."&d=".$countingdays."\" />".$countingdays."</a>";}?></div></body>

This line seems to be executed even if the statment isn't true.

if($countingdays == $selected){	$class = $class." selected";}

Really odd is what if I select 8th of the month it works but not 20thI think is some bug couse it works and it don't at the same time.I have no clue what to do.

Link to comment
Share on other sites

This might be a type issue, in some places you're treating numbers as numbers and in other places you're treating strings as numbers. For example, with this:$day = date("d");$day--;The date function returns a string, not a number. When the format string is "d", for the 8th it will return the string "08", not the number 8. If you want to work with numbers, it's best to use the date format strings "j" and "n" instead of "d" and "m", and use intval to cast the string to a number before trying to do things like math with it.The same issue with this:date("Ym") >= $year.$monthKeep in mind that's a string comparison (alphabetic), not a numeric comparison. In a string comparison, "20092" is greater than "200912".Also, you're ending your <a> tag too early.

Link to comment
Share on other sites

This might be a type issue, in some places you're treating numbers as numbers and in other places you're treating strings as numbers. For example, with this:$day = date("d");$day--;The date function returns a string, not a number. When the format string is "d", for the 8th it will return the string "08", not the number 8. If you want to work with numbers, it's best to use the date format strings "j" and "n" instead of "d" and "m", and use intval to cast the string to a number before trying to do things like math with it.The same issue with this:date("Ym") >= $year.$monthKeep in mind that's a string comparison (alphabetic), not a numeric comparison. In a string comparison, "20092" is greater than "200912".Also, you're ending your <a> tag too early.
Dude and I thought I could code.. well now I see I'm not so good as I thought. So to the bit where I learn something.. What do you mean with ending my <a> tag to early..How do I do a numeric comparison with a string?
Link to comment
Share on other sites

Well to calculate time, I always use Unixtime. It gives the current number second of secondes since (whatever) in the form of: 1258600568 witch is 2009-11-19 03:16:08 when I convert it. Using this Unixtime you can calculate to the second on what time it is, or will be in 3 days:http://www.php.net/manual/en/function.strtotime.php gives the unix timestamp when you input an english textual time.

Link to comment
Share on other sites

This tag:echo "<a class=\"".$class."\" href=\"?y=".$year."&m=".$month."&d=".$countingdays."\" />".$countingdays."</a>";Is a self-closing XHTML-style tag, then text, then a closing </a>. The last slash in the first part of the tag self-closes the tag.You can use intval to convert all of the strings to numbers and then compare numbers:$day = intval(date("j"));$day--;intval(date("Yn")) == intval($year.$month)

Link to comment
Share on other sites

This tag:echo "<a class=\"".$class."\" href=\"?y=".$year."&m=".$month."&d=".$countingdays."\" />".$countingdays."</a>";Is a self-closing XHTML-style tag, then text, then a closing </a>. The last slash in the first part of the tag self-closes the tag.You can use intval to convert all of the strings to numbers and then compare numbers:$day = intval(date("j"));$day--;intval(date("Yn")) == intval($year.$month)
There's also PHP's idate() function which returns an integerEdit: Though idate() only works with a single character in the $format parameter. Edited by Ingolme
Link to comment
Share on other sites

This tag:echo "<a class=\"".$class."\" href=\"?y=".$year."&m=".$month."&d=".$countingdays."\" />".$countingdays."</a>";Is a self-closing XHTML-style tag, then text, then a closing </a>. The last slash in the first part of the tag self-closes the tag.You can use intval to convert all of the strings to numbers and then compare numbers:$day = intval(date("j"));$day--;intval(date("Yn")) == intval($year.$month)
Okay how funny that I had closed the tag to early most have thought of images then I wrote the <a> tag.Well my problem is still here
<head>	<title>		Blog	</title>	<link rel="stylesheet" href="blog.css" type="text/css"></head><body><div id="calender"><?phpif(isset($_GET['y']) and $_GET['y'] != ""){	$year = $_GET['y'];}else{	$year = intval(date("Y"));}if(isset($_GET['m']) and $_GET['m'] != ""){	$month = $_GET['m'];}else{	$month = intval(date("n"));}if(isset($_GET['d']) and $_GET['d'] != ""){	$selected = $_GET['d'];}else{	$selected = intval(date("j"));}$day = intval(date("j"));$day--;$numberofdays = cal_days_in_month(CAL_GREGORIAN, $month, $year);$countingdays = 0;while($numberofdays != $countingdays){		if($countingdays == $day and intval(date("Ym")) == intval($year.$month)){		$class = "today";	}else if($countingdays < $day and intval(date("Ym")) >= intval($year.$month)){		$class = "pastdays";	}else if(intval(date("Ym")) <= intval($year.$month)){		$class = "comingdays";	}		$countingdays++;	if($countingdays == $selected){		$class = $class." selected";	}	echo "<a class=\"".$class."\" href=\"?y=".$year."&m=".$month."&d=".$countingdays."\">".$countingdays."</a>";}?></div></body>

Edited by ckrudelux
Link to comment
Share on other sites

Okay now I know what isn't working and why.. have to rewrite this line

}else if($countingdays < $day and intval(date("Ym")) >= intval($year.$month)){

couse now it thinks when you get to the day in this month on a past month it still thinks it's this month and this results in that the class tag never changes after that date.

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...