Forum Moderators: coopster
I'm trying to figure out how to calculate the duration when given 2 times.
What I have is a script shown below in which you select a start time and end time from a drop down box, but would like some assistance on how to calculate the duration.
An example:
Start Time: 8:30
End Time: 9:30
Result: 1 Hour (60 mins)
<tr>
<td><b>Select Operation Start Time</b></td>
<td><select name="start_time">
<option value="null">Select One</option>
<?
$start=mktime(8,30,0,date('m'),date('d'),date('Y'));
for($i=$start;$i<=$start+32400;$i+=900)
{
$time=date("H:i",$i);
echo "<option value=$time>$time</option>";
}
?>
</select></td>
</tr>
<tr>
<td><b>Select Operation End Time</b></td>
<td><select name="end_time">
<option value="null">Select One</option>
<?
$start=mktime(8,30,0,date('m'),date('d'),date('Y'));
for($i=$start;$i<=$start+32400;$i+=900)
{
$time=date("H:i",$i);
echo "<option value=$time>$time</option>";
}
?>
</select></td>
</tr>
Thanks again. :o)
<tr>
<td><b>Select Operation Start Time</b></td>
<td><select name="start_time">
<option value="null">Select One</option>
<?
$start=mktime(8,30,0,date('m'),date('d'),date('Y'));
for($i=$start;$i<=$start+32400;$i+=900)
{
$time=date("H:i",$i);
echo "<option value=\"$i\">$time</option>";
}
?>
</select></td>
</tr>
<tr>
<td><b>Select Operation End Time</b></td>
<td><select name="end_time">
<option value="null">Select One</option>
<?
$start=mktime(8,30,0,date('m'),date('d'),date('Y'));
for($i=$start;$i<=$start+32400;$i+=900)
{
$time=date("H:i",$i);
echo "<option value=\"$i\">$time</option>";
}
?>
</select></td>
</tr>
When you compare the 2 times after the form has been submitted just minus the start time from the end time and do a little bit of math to get the result you are after:
$seconds = $_POST['end_time'] - $_POST['start_time'];
$minutes = ceil($seconds/60);
$hours = ceil($minutes/60);
or you can just jump straight to the hour difference:
$hours = (($_POST['end_time'] - $_POST['start_time'])/60)/60;