Jump to content

bcdiv


ZeroShade
 Share

Recommended Posts

I have a php page that will simply take some information from the user and output the time it will take for the train to travel the specified distance. But if the answer 2.5 is printed... it really means 2 hours and 30 minutes. I understand that the bcdiv can fix this somehow? Any ideas as to how to use it with what I have?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">		<head>				<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />								<link rel="stylesheet" href="styles.css" type="text/css" />				<title>Passenger Train</title>		</head>		<body>							 	<h1>Passenger Train</h1>								<h2>Calculate Time</h2>								<p />This service allows you to calculate the time it will take to reach your destination on our Passenger Train's. Please enter the distance you will be travelling, how many stops will occur, and if there is good or bad weather. You may use this service as many times as you wish. Have fun! 				<form action="PassengerTrain.php" method="post">						<?php								$speed = 50;								$weatherDecrease = 40;								$stop = 60 - ($numberOfStops * 5);								$miles = ($_POST['miles']);								$numberOfStops = ($_POST['numberOfStops']);																$weather = ($_POST['weather']);								if(isset($_POST['submit']))								{										if(ctype_digit($miles) != true || ctype_digit($numberOfStops) != true)												echo "<p />Please fill in your information appropriately. Values must be numeric.";										else										{												if($weather == '1')														$result = $stop * $miles / $speed;												else if($weather == '2')														$result = $stop * $miles / $weatherDecrease;														// divide 70 miles by 50 speed... minutes equals result - trimmed result * 60														//bcdiv(string $left_operand, string, string $right_operand [, int $scale])										}								}								echo "<p />Miles:<input type='text' name='miles' value='' size='1' maxlength='3' />";								echo "<p />Number of stops:<input type='text' name='numberOfStops' value='' size='1' maxlength='3' />";								echo "<p />Good weather:<input type='radio' name='weather' value='1' checked='checked' />";								echo "<p />Bad weather:<input type='radio' name='weather' value='2' />";								echo "<input type='submit' value='Calculate Time' name='submit' />";								echo $result;						?>				</form>								<img src="train.jpg" alt="" />		</body></html>

Link to comment
Share on other sites

PHP always measures in seconds.

$seconds = 60; // 1 minute$minutes = $seconds / 60; // Convert to Minutes$hour = $minutes / 60; // Convert to Hours.

You can convert hours back to minutes by timsing hours by 60. For example, 120 minutes is 2 hours.

Link to comment
Share on other sites

PHP does not always measure in seconds, and even if it did, he's not using any PHP time functions, he's just multiplying numbers. You also don't need to use bcdiv though, in fact I can't really see any use for bcdiv other then a combination of division and rounding. I told you in another thread though how to convert a floating point number to hours and minutes, did you see that?Here it is:

$time = floatval($time_str);$hours = intval($time_str);$mins = round(($time - $hours) * 60);

$time_str is what you get from $_POST or $_GET, or the result of another calculation. If $time_str started at 2.5, then $hours would be 2 and $mins would be 30.

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