Page 1 of 1

Problem with date functions

Posted: Thu Oct 26, 2006 1:30 am
by travtaylor
Hi all-

I'm having a bit of a rough time debugging a chunk of code from an application written by someone who's currently out of contact... it's a calendar-based application allowing with forms in each day, allowing people to select specific sets of options. This has been having an ongoing glitch which revolves around the date function - specifically the following block of code:

Code: Select all

$datetemp = getdate();
if ($_POST['eday'] && $month_num != $datetemp['mon']) {
	$eday = $_POST['eday'];
	$edayend = $_POST['edayend'];
	$edayend = $edayend-$eday;
} else { 
	$todaytemp = $datetemp['mday']; 
	$weekdaytemp = $datetemp['wday'];
	if ( $weekdaytemp == 0) { $todaytemp +1; }
	$eday = $todaytemp+5;
	$edayend = $eday+5;
	while ($weekdaytemp > 1 ) {
		$weekdaytemp--;
		$todaytemp--;
	}
}
It's not necessarily that this code has a specific syntax error as it is that I'm having difficulty deciphering how it works. It's part of a block of code that makes users unable to alter their food selections two weeks from the present date. I've looked up a lot about date functions on php.net, and I've figured out a bit about it, but as I said, something about how this works isn't clicking - I was hoping someone could explain it a little bit further.

Thanks!

Posted: Thu Oct 26, 2006 4:17 am
by Mordred
It seems to me that this code heavily mixes apples and oranges, after the if() part, the $edayend seems to be an interval, while after the else part, $edayend is a date.

Strange snippets:

Code: Select all

while ($weekdaytemp > 1 ) { 
                $weekdaytemp--; 
                $todaytemp--; 
        }
is actually a stupid way to say

Code: Select all

$todaytemp -= $weekdaytemp+1; 
$weekdaytemp = 1;

Code: Select all

if ( $weekdaytemp == 0) { $todaytemp +1; }
This doesn't do anything. Maybe it was supposed to be $todaytemp++?

It seems this code aims to find the previous monday, only if it is sunday, it finds the NEXT monday.
This has no impact on $eday and $eday end though. The if() condition also seems unrelated to the task, whatever that is.

Do you know what it is supposed to do?