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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

No Idea...

Status
Not open for further replies.

SBuzzT

Programmer
Aug 24, 2005
86
CA
I tried to do something which I initially thought would not be very difficult, but silly me... Here's the formula:

Code:
$a = $_GET['a'];
$b = $a+15;
$c = $b+14;
$d = $c+17;
$e = $d+11;
$f = $e+17;

What I want is to put in 6 input fields. By filling in any of the 6, the others will calculate automatically (when the forms submits). Here's the catch... they have to be date strings (Mar-3-2008). I have the snippet of code below to convert any date input to the format I want, but now my brain has taken a vacation. If anyone can set me in the right direction, I would really appreciate it.

Code:
$input = $_GET["date"];
$bill  = date( 'M-j-Y', strtotime($input));
echo $bill;
{/code]
 
you have not explained what you're trying to achieve and the posting above bears no resemblance to date manipulation that would allow us to guess at what you're attempting

can you try explaining again?
 
If your inputs are text boxes, they will be submitted as an empty string. So you can use something like:

Code:
if($_GET['b']==''):
   // formula with input
else:
   // formula without input
endif;

Does this help?

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
When I put a date in field A, fields B through F would calculate automatically. Likewise, if I put a date in field C (or any other) the remaining fields would calculate automatically.

For example, typing a date into field C (jan-30-2008) would generate the dates for the remaining fields.
Code:
    A              B            C            D     
jan-1-2008   jan-16-2008   jan-30-2008  feb-16-2008 ... and so on.
 
i'm not sure i even remotely understand the reason why you might want this kind of variable date interval, but here is some code that will work. and even take into a account leap years (which your response above, did not).

Code:
<?php
$fields = array('a','b','c','d','e','f');
if (isset($_GET['submit'])){
	processInput();
} else {
	renderForm();
}
function processInput(){
	global $fields;
	$intervals = array(0, 15, 29 ,44, 55, 72);
	$counter = 0;
	//assume earliest field wins
	foreach ($fields as $field) {
		if (empty($_GET[$field])){
			$counter++;
			//do nothing
		} else {
			$t = strtotime($_GET[$field]);
			if ($t == false || $t === -1){
				echo $t;
				$counter ++;
			} else {
				//get interval position
				$i = array_search($field, $fields);
				$curField = $field;
				break;
			}
		}
	}
	if ($i === false) { //no valid date
		renderForm(array(), "You must enter a valid date (YYYY-mm-dd)");
	}
	//now derive initial value

	$baseValue = strtotime ('-' . $intervals[$i] . ' day', strtotime($_GET[$curField]));
	
	for ($v=0; $v < count ($intervals); $v++){
		$values[$fields[$v]] = date ('Y-m-d', strtotime('+' . $intervals[$v] . ' day', $baseValue));
	}
	
	renderForm($values);
}

function renderForm($values = array(), $message = NULL){
	global $fields;
	$output = '';
	foreach ($fields as $field){
		$v = (isset($values[$field])) ?  $values[$field]  : '';
		$output .= <<<HTML
		
		<div class="field">
			<input type="text" name="$field" value="$v" />
		</div>

HTML;
	}
	if (!empty($message)){
		$message = <<<HTML
	<div class='errorMessage'>
	$message
	</div>

HTML;
	}
	echo <<<HTML
	$message
<form method="get" action="{$_SERVER['PHP_SELF']}">
	<fieldset>
		$output
		<div>
			<input type="submit" name="submit" value="submit" />
		</div>
	</fieldset>
</form>
HTML;
	exit(); //kill the process
}
?>

 
I'm not sure how you did this so fast, but other than a slight math mistake, it's perfect. Impressive... And thanks a bunch.

The reason I use this is for calculating dates for a work project. It will just save me the trouble of doing it manually.

Thanks a million. :)
 
maths mistake. hmmm! not admitting that yet. the delta between your results and mine looked to be linked to the leap year.

but anyway, glad to have helped.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top