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

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?

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.

  • 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;

?>

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

Archived

This topic is now archived and is closed to further replies.

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