Welcome to WebmasterWorld Guest from 54.197.171.28

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Calculating Duration

   
5:01 pm on Feb 27, 2005 (gmt 0)

10+ Year Member



Hi,

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)

9:19 pm on Feb 27, 2005 (gmt 0)

10+ Year Member



Compare the time using the time stamps you have created using the mktime() function. In your example I've replaced the option tag values with the $i variable (which contains your unix timestamp).

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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month