PHP: Convert user input in minutes to some use-able value for a calculation
Overall goal: User enters some number in minutes and I change those numbers开发者_如何转开发 from minutes to some usable form in a calculation.
How do I convert the user input in the form of minutes to some number or value that I can use later on in this php script?
<?php
$minutes = $_REQUEST["minutes"];
// We initialize the total to $1.99 for the connection fee:
$total = 1.99;
// For each additional minute we add $0.45?
// For example if 3 minutes are used the cost = $2,
$addmin = 0.45;
/* How do I convert the user inputted minutes to some value
I can manipulate with php so that I can just add via php and give the user
a cost, for example, 3minutes entered into a form might be = cost of $2.
What do I need to read or study?*/
//We use a if statement here:
if (filter_has_var(INPUT_POST, "minutes"))
{
// If the user selects the number of minutes, we echo out the number of minutes.
print $minutes;
print "mins.";
// Condition pending here:
$total += filter_input(INPUT_POST, "$addmin");
}
print "<p>The total cost is: \$$total</p> \n";
?>
Minutes are already a usable form of number for a calculation. If you use the $minutes variable in a calculation it should automatically be cast to a number.
// $minutes = 3, $addmin = 0.45, 3 * 0.45 = 1.35
$subtotal = $minutes * $addmin;
$total += $subtotal;
If you wanted the first 3 minutes to be $2 and then each additional minute to be $0.45, you'd do something like the following:
$subtotal = 0;
// If less than 3 minutes were input, use 3 minutes anyway.
$minutes = $minutes < 3 ? 3 : $minutes;
if ($minutes >= 3) // If minutes
{
$minutes -= 3;
$subtotal = 2;
}
// multiple the remaining minutes by the additional cost
$subtotal += $minutes * $addmin;
$total += $subtotal;
why don't you just convert everything in seconds and use the number of seconds and the cost per seconds to do any computation ? When you are done you can convert again from seconds to minutes and display your results.
精彩评论