Subtract two times from each other

Hey,

I have two times entered into a form, a starting time and an ending time e.g, Start Time: 3:30, End Time: 5:15.

I enter these by hand, so I can enter them like 3.3 and 5.15 or however needed, but what I need to do is minus the end time from the start time to see how long I’ve worked so I can then see how much I’ve made per hour or whatnot…

so I would somehow need to be able to minus 3.3 from 5.15 and get the minute difference between the two(5:15-3:30, I would want to get 105 minute difference)

I’ve looked up strtotime, mktime, etc, but I have no idea how to get the minutes out of unix timestamps so any help would be greatly appreciated…

Thanks a bunch,

PHP’s [fphp]time/fphp is measure in seconds

so record time() at the start and record time() at the end… that will give you the difference in seconds. To get the difference in minutes, just divide by 60.

Hey,

I would be entering both at the same time, and would have to enter them manually because like otherwise I don’t know how to do it…I would be putting in the starting time, then playing a game for however long I played it, then I would get the ending time, so how would I get each seperately?

Thanks again

Off Topic:

You play games for a living? man, I want your job! :slight_smile:

Here is a full working page I whipped up for you to play with:


<?
$start_time = $_REQUEST['start_time'];
$end_time = $_REQUEST['end_time'];
$action = $_REQUEST['action'];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body>
<h3>Parse Times</h3>
<form action="<?= $_SERVER['PHP_SELF'] ?>">
Enter the start time (hours:minutes)
<input type="text" name="start_time" value="<?= $start_time ?>" size="60"> <br/>

Enter the end time (hours:minutes)
<input type="text" name="end_time" value="<?= $end_time ?>" size="60"> <br/>
<input type="submit" value="Go!"> <br/>

<input type="hidden" name="action" value="go">
</form>
<hr>

<?
if($action && ($action == "go")){
	list($hours, $minutes) = split(':', $start_time);
	$startTimestamp = mktime($hours, $minutes);
	
	list($hours, $minutes) = split(':', $end_time);
	$endTimestamp = mktime($hours, $minutes);
	
	$seconds = $endTimestamp - $startTimestamp;
	$minutes = ($seconds / 60) % 60;
	$hours = round($seconds / (60 * 60));
	
	echo "Time passed: <b>$hours</b> hours and <b>$minutes</b> minutes";
}
?>
</body>
</html>


Wow! Thanks man, I didn’t expect that kinda work but that really helps! I will try it out and let you know how it goes…Thanks again!

P.S. I play poker for a living, seems like a game to me because I enjoy doing it, but it gets to be a job because sometimes it can get pretty boring :slight_smile:

Thanks again

Hey,

I’m getting some kind of problem with it…If I put in 2:15 and 6:30, I get 4 hours and 15 minutes(good to hear!) but if I put in 2:15 and 6:10, I get 4 hrs and 55 minutes, instead of 3 hrs and 55 minutes…

Thanks again

use floor() to get the hours instead of round().

Good one, I didn’t realize that.

Worked, thanks guys :slight_smile: