VinceGledhill Posted April 14, 2011 Share Posted April 14, 2011 Hello People. Please can someone point me to where I can learn to program the following all one one page? Here is a screenshot of what I'm after. Idea being that you put hours and minutes into the top row. Then hours and minutes into the next row. Click the button and get the answer at the bottom. The answer to 1 hour 40 minutes added to 1 hour 40 minutes should obviously be 3 hours 20 minutes. Here's how I would approach it in another language, but am brand new to php hence the help required. Make the following variables. Hour1 Hour2 Minute1 Minute2 Total_Hours Total_Minutes Combined_Time I would then write something like Total_minutes = minute1 + Minute2 Total_hours = Hour1 + hour2 combined time = Total_hours * 60 + (Total_Minutes) // reset the total hours Total_hours = 0 Loop here.... while combined_time >60 Total_hours = Total_hours + 1 combined_time = combined time -60 re-do the loop Now in the boxes at the bottom put bottom hour box = total hours bottom minute box = combined_time (remaining minutes) here's the code I have so far. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <table width="527" border="0" cellspacing="5" cellpadding="5" bgcolor="#c0c5c9"> <p>Time Calculator</p> <p>Put your hours and minutes in the top boxes. Click the add time button<br /> to get the answer </p> <table width="401" border="1" cellspacing="5" cellpadding="5"> <tr> <td width="109"><input type="int" name="hour01" size="10" maxlength="10" /> Hrs</td> <td width="251"><input type="int" name="min01" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour02" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="min02" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="submit" name="submit" value="Add Time" /></td> <td><input name="hour_answer" type="int" size="10" maxlength="10" /> Hrs <input type="int" name="minute_answer" size="10" maxlength="10" /> Mins</td> </tr> </table> <tr></tr> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/233703-please-can-someone-show-me-how-to-code-a-simple-addition-all-on-the-same-page/ Share on other sites More sharing options...
Cagecrawler Posted April 14, 2011 Share Posted April 14, 2011 Your math would work, but an easier way is to say: totalMins = combinedTime % 60; totalHours = floor(combinedTime / 60); As for getting the text where you want it, look into either working with html forms in php or using javascript to process the page without reloading. Quote Link to comment https://forums.phpfreaks.com/topic/233703-please-can-someone-show-me-how-to-code-a-simple-addition-all-on-the-same-page/#findComment-1201545 Share on other sites More sharing options...
QuickOldCar Posted April 14, 2011 Share Posted April 14, 2011 Here's a simple example There is no checking for errors or numeric numbers this code, which you should do. You can even add seconds to this, expand it to days,months or years even. Add hours and minutes <form action="" method="post" enctype="multipart/form-data"> Hours: <input type="text" name="hour[]" /> Minutes: <input type="text" name="minute[]" /><br /> Hours: <input type="text" name="hour[]" /> Minutes: <input type="text" name="minute[]" /><br /> <input type="submit" value="Add Them" /> </form> <?php $hours = $_POST['hour']; $minutes = $_POST['minute']; function convertMinSec($number_value) { $divided = floor($number_value / 60); return $divided; } $addminutes = $minutes['0'] + $minutes['1']; $hoursfromminutes = convertMinSec($addminutes); $deduct_amount = 60 * $hoursfromminutes; $totalminutes = $addminutes - $deduct_amount; $totalhours = $hours['0'] + $hours['1'] + $hoursfromminutes; echo $hours['0']." hours ".$minutes['0']." minutes<br />"; echo $hours['1']." hours ".$minutes['1']." minutes<br /><hr />"; echo "Total $totalhours hours $totalminutes minutes<br />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/233703-please-can-someone-show-me-how-to-code-a-simple-addition-all-on-the-same-page/#findComment-1201633 Share on other sites More sharing options...
QuickOldCar Posted April 14, 2011 Share Posted April 14, 2011 I redid this to place into your form, also changes the form a little to fix it. <?php $hours = $_POST['hour']; $minutes = $_POST['minute']; function convertMinSec($number_value) { $divided = floor($number_value / 60); return $divided; } $addminutes = $minutes['0'] + $minutes['1']; $hoursfromminutes = convertMinSec($addminutes); $deduct_amount = 60 * $hoursfromminutes; $totalminutes = $addminutes - $deduct_amount; $totalhours = $hours['0'] + $hours['1'] + $hoursfromminutes; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <form action="" method="post" enctype="multipart/form-data"> <table width="527" border="0" cellspacing="5" cellpadding="5" bgcolor="#c0c5c9"> <p>Time Calculator</p> <p>Put your hours and minutes in the top boxes. Click the add time button<br /> to get the answer </p> <table width="401" border="1" cellspacing="5" cellpadding="5"> <tr> <td width="119"><input type="int" name="hour[]" value="<?php echo $hours['0'];?>"size="10" maxlength="10" /> Hrs</td> <td width="241"><input type="int" name="minute[]" value="<?php echo $minutes['0'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]" value="<?php echo $hours['1'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]" value="<?php echo $minutes['1'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="submit" name="submit" value="Add Time" /></td> <td><input value="<?php echo $totalhours;?>" type="int" size="10" maxlength="10" /> Hrs <input type="int" value="<?php echo $totalminutes;?>" size="10" maxlength="10" /> Mins</td> </tr> </table> <tr></tr> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/233703-please-can-someone-show-me-how-to-code-a-simple-addition-all-on-the-same-page/#findComment-1201648 Share on other sites More sharing options...
VinceGledhill Posted April 14, 2011 Author Share Posted April 14, 2011 Absolutely brilliant mate. Finished form can now be found here http://www.microlight.co/index.php/calculator Thank you from the bottom of my heart. Quote Link to comment https://forums.phpfreaks.com/topic/233703-please-can-someone-show-me-how-to-code-a-simple-addition-all-on-the-same-page/#findComment-1201749 Share on other sites More sharing options...
QuickOldCar Posted April 15, 2011 Share Posted April 15, 2011 I revised this even more and made it to do whole or decimal numbers. As for the seconds or decimals for the minutes, I rounded them. I was going to make a selection on how many input fields to do and do it dynamic versus assigning them, but I think already spent too much time on this. Was certainly challenging though, it was fun. You're just seeing the working one, and not all the test versions. I tried it a while and seems pretty good and accurate. Here's a demo http://dynainternet.com/flight-log.php Code: <?php $hours = $_POST['hour']; $minutes = $_POST['minute']; if($_POST['hour'] != '' || $_POST['minute'] != '') { function decHrMinSec($number){ if(strpos($number,".") !== false){ $number_explode = explode(".", $number); $pos_zero = $number_explode['0']; $round_pos_one = round($number_explode['1'],2); $pos_one = substr($round_pos_one,0,2); if(strlen($pos_one) != range(1,2)) { $pos_one = $pos_one * 10; } $multiply_sixty = $pos_zero * 60; //multiply_sixty = floor($pos_zero * 60); $total_result = $pos_one + $multiply_sixty; } else { $total_result = $number * 60; } return $total_result; } function convertMinSec($number_value) { $divided = floor($number_value / 60); return $divided; } $sum_minutes = array_sum($_POST['hour']); $finalminutes = decHrMinSec($sum_minutes); $addminutes = array_sum($_POST['minute']) + $finalminutes; $totalhours = convertMinSec($addminutes); $deduct_amount = 60 * $totalhours; $allminutes=$addminutes - $deduct_amount; $totalminutes = round($allminutes); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Flight Log Calculator</title> </head> <body> <form action="" method="post" enctype="multipart/form-data"> Pilots Logbook Calculator<br /> <p>Put your times in the hours and minutes boxes then click the "Add Time" button to get your answer. </p> <table width="401" border="1" cellspacing="5" cellpadding="5"> <tr> <td width="119"><input type="int" name="hour[]" value="<?php echo $hours['0'];?>"size="10" maxlength="10" /> Hrs</td> <td width="241"><input type="int" name="minute[]" value="<?php echo $minutes['0'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]" value="<?php echo $hours['1'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]" value="<?php echo $minutes['1'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]2" value="<?php echo $hours['2'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]2" value="<?php echo $minutes['2'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]3" value="<?php echo $hours['3'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]3" value="<?php echo $minutes['3'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]4" value="<?php echo $hours['4'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]4" value="<?php echo $minutes['4'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]5" value="<?php echo $hours['5'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]5" value="<?php echo $minutes['5'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]6" value="<?php echo $hours['6'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]6" value="<?php echo $minutes['6'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]7" value="<?php echo $hours['7'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]7" value="<?php echo $minutes['7'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]8" value="<?php echo $hours['8'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]8" value="<?php echo $minutes['8'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]9" value="<?php echo $hours['9'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]9" value="<?php echo $minutes['9'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]10" value="<?php echo $hours['10'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]10" value="<?php echo $minutes['10'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]11" value="<?php echo $hours['11'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]11" value="<?php echo $minutes['11'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="submit" name="submit" value="Add Time" /></td> <td><input value="<?php echo $totalhours;?>" type="int" size="10" maxlength="10" /> Hrs <input type="int" value="<?php echo $totalminutes;?>" size="10" maxlength="10" /> Mins</td> </tr> </table> <tr></tr> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/233703-please-can-someone-show-me-how-to-code-a-simple-addition-all-on-the-same-page/#findComment-1202178 Share on other sites More sharing options...
QuickOldCar Posted April 15, 2011 Share Posted April 15, 2011 I made a fix for certain conditions of a decimal in the function. <?php $hours = $_POST['hour']; $minutes = $_POST['minute']; if($_POST['hour'] != '' || $_POST['minute'] != '') { function decHrMinSec($number){ if(strpos($number,".") !== false){ $number_explode = explode(".", $number); $pos_zero = $number_explode['0']; $round_pos_one = round($number_explode['1'],2); $pos_one = substr($round_pos_one,0,2); if(strlen($pos_one) == range(1,2) && strlen($pos_one) != '') { $pos_one = $pos_one * 10; } $multiply_sixty = $pos_zero * 60; //multiply_sixty = floor($pos_zero * 60); $total_result = $pos_one + $multiply_sixty; } else { $total_result = $number * 60; } return $total_result; } function convertMinSec($number_value) { $divided = floor($number_value / 60); return $divided; } $sum_minutes = array_sum(array_filter($_POST['hour'])); $finalminutes = decHrMinSec($sum_minutes); $addminutes = array_sum(array_filter($_POST['minute'])) + $finalminutes; $totalhours = convertMinSec($addminutes); $deduct_amount = 60 * $totalhours; $allminutes=$addminutes - $deduct_amount; $totalminutes = round($allminutes); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Flight Log Calculator</title> </head> <body> <form action="" method="post" enctype="multipart/form-data"> Pilots Logbook Calculator<br /> <p>Put your times in the hours and minutes boxes then click the "Add Time" button to get your answer. </p> <table width="401" border="1" cellspacing="5" cellpadding="5"> <tr> <td width="119"><input type="int" name="hour[]" value="<?php echo $hours['0'];?>"size="10" maxlength="10" /> Hrs</td> <td width="241"><input type="int" name="minute[]" value="<?php echo $minutes['0'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]" value="<?php echo $hours['1'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]" value="<?php echo $minutes['1'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]2" value="<?php echo $hours['2'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]2" value="<?php echo $minutes['2'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]3" value="<?php echo $hours['3'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]3" value="<?php echo $minutes['3'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]4" value="<?php echo $hours['4'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]4" value="<?php echo $minutes['4'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]5" value="<?php echo $hours['5'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]5" value="<?php echo $minutes['5'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]6" value="<?php echo $hours['6'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]6" value="<?php echo $minutes['6'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]7" value="<?php echo $hours['7'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]7" value="<?php echo $minutes['7'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]8" value="<?php echo $hours['8'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]8" value="<?php echo $minutes['8'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]9" value="<?php echo $hours['9'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]9" value="<?php echo $minutes['9'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]10" value="<?php echo $hours['10'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]10" value="<?php echo $minutes['10'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="int" name="hour[]11" value="<?php echo $hours['11'];?>" size="10" maxlength="10" /> Hrs</td> <td><input type="int" name="minute[]11" value="<?php echo $minutes['11'];?>" size="10" maxlength="10" /> Mins</td> </tr> <tr> <td><input type="submit" name="submit" value="Add Time" /></td> <td><input value="<?php echo $totalhours;?>" type="int" size="10" maxlength="10" /> Hrs <input type="int" value="<?php echo $totalminutes;?>" size="10" maxlength="10" /> Mins</td> </tr> </table> <tr></tr> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/233703-please-can-someone-show-me-how-to-code-a-simple-addition-all-on-the-same-page/#findComment-1202183 Share on other sites More sharing options...
QuickOldCar Posted April 16, 2011 Share Posted April 16, 2011 Well when doing many big numbers using a combination of decimals and whole numbers it gets a little off due to the converting process of each. I did manage to make one more accurate though. Code is below on page. http://get.blogdns.com/logged.php I have another with a different method, then also another in the works using mostly explode. (we'll see how that goes, or if I get frustrated enough... say "that's good enough already") I'm going to convert them all down to seconds, then convert to minutes, then convert to hours. Add the remainder minutes from hours back onto minutes. Quote Link to comment https://forums.phpfreaks.com/topic/233703-please-can-someone-show-me-how-to-code-a-simple-addition-all-on-the-same-page/#findComment-1202280 Share on other sites More sharing options...
VinceGledhill Posted April 16, 2011 Author Share Posted April 16, 2011 Brilliant, thanks mate, you really have had some fun with that question. What's the difference with the first one and this latest one? The first one as posted on my site "works"? Quote Link to comment https://forums.phpfreaks.com/topic/233703-please-can-someone-show-me-how-to-code-a-simple-addition-all-on-the-same-page/#findComment-1202347 Share on other sites More sharing options...
QuickOldCar Posted April 16, 2011 Share Posted April 16, 2011 For this one... http://get.blogdns.com/logged.php .25 hours equals 15 minutes .5 is 30 minutes .75 is 45 minutes and then everything in between like .99 hours being 59 minutes So basically it equates something like 1.7 hours as 1 hour and 42 minutes The first one.. and at your site sees 1.7 hours as 1.7 hours and doesn't convert the .7 to minutes Quote Link to comment https://forums.phpfreaks.com/topic/233703-please-can-someone-show-me-how-to-code-a-simple-addition-all-on-the-same-page/#findComment-1202397 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.