Jump to content

PHP Calculate finish time from start time and duration


doop

Recommended Posts

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  :)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 4 weeks later...

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;

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.