doop Posted October 16, 2007 Share Posted October 16, 2007 Hi Guys, I have been having trouble finding something on how to calculate a finish time from a start time and duration. I have found various scripts from start and finish time to equal duration but really need it the other way around. Any help/point in the right direction would be great thanks Quote Link to comment Share on other sites More sharing options...
btherl Posted October 16, 2007 Share Posted October 16, 2007 I really don't understand what you want. Can you give an example? I would have thought finish time is start time + duration, but it's more complex than that? Quote Link to comment Share on other sites More sharing options...
doop Posted October 16, 2007 Author Share Posted October 16, 2007 Hi, I really should have included an example of my though process. What I basically need is: Say you enter a start time of 14:00 and you enter a duration of 30 minutes. Rather than entering the finish time of 14:30, I want something to calculate this automatically. Does that make more sense? Quote Link to comment Share on other sites More sharing options...
btherl Posted October 16, 2007 Share Posted October 16, 2007 Yes that makes sense. I would convert both to seconds and then do the calculation, then convert back. You can use the date and time functions. I'm not sure exactly which you'll need, but strtotime() is often useful. So is mktime(). The final one is date(), which can format your timestamp as a date again. Quote Link to comment Share on other sites More sharing options...
Alex020691 Posted November 10, 2007 Share Posted November 10, 2007 I've only been doing php for a couple of days, so apologies if this is wrong, it seems to work when I've been testing it. I KNOW this isn't the most efficient/best way, but its the only way I could do it with my limited knowledge . <html> <body> <b><u>Finishing time finder</u></b><br><br> <form method='post' action='duration2.php' name='starttime'> <table cellpadding=2 cellspacing=2> <tr> <td> Start time [hh:mm]</td><td> <input type='text' name='start' maxlength='5' size='5'></td></tr> <tr><td> Duration [hh:mm] </td><td> <input type='text' name='duration' maxlength='5' size='5'></td></tr> <tr><td> <input type='submit' name='submit' value='Find finishing time'> </td><td> <input type='reset' name='reset' value='Reset fields'></td></tr> </table> </form> <br> <?php $start = $_POST['start']; $duration = $_POST['duration']; $startM = explode(":", $start); $durationM = explode(":", $duration); $array0 = $startM[0]+$durationM[0]; $array1 = $startM[1]+$durationM[1]; if ($array1>=60) { $array1 = $array1-60; $array0 = $array0+1; } while ($array0>=24) $array0 = $array0-24; echo 'Your finishing time is: '; if ($array0>=10) echo $array0; else echo '0'.$array0; echo ':'; if ($array1>=10) echo $array1; else echo '0'.$array1; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted November 10, 2007 Share Posted November 10, 2007 or you take btherl's tips and <?php $start = '14:00'; $duration = 30; $end = strtotime($start) + $duration * 60; echo date ('H:i', $end ); // --> 14:30 ?> Quote Link to comment 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.