homepage Welcome to WebmasterWorld Guest from 54.211.219.178
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Calculating Duration
woldie




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

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)

 

ironik




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

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;

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved