Insert Duration from Users Form

This can't be that hard but after hours of reading and testing I've got very comfused and a little angry with my PC, I know it's not its fault really....

What I need is a way to insert the duration time from two input text boxes named/ID'ed TripStart and TripEnd, when the form is posted all I need is a calculation of time passed from TripStart and TripEnd.

i.e. user enters 10:30 and 12:30, I need both those added to database (can do this ok), but also in another field called Duration the results of the calculation, 2:00. should the time be entered 23:30 and 03:30 the value added should be 4:00.

Any help would be good, pretty good at the database side of things, its the PHP that is killing me, lol...

High points as need a fast and clear to follow method, willing to increase points if needed.
Who is Participating?
yauhingConnect With a Mentor Commented:
Please find below example:
function calc($start, $end){
	$a = split(":", $start);
	$b = split(":", $end);
	$start_min = $a[0] * 60 + $a[1];
	$end_min = $b[0] * 60 + $b[1];
	if ($end_min < $start_min)	$end_min += (24 * 60); 
	$diff_min = $end_min - $start_min;
	return intval($diff_min / 60).":". substr("0".($diff_min % 60), -2);
$TripStart = "23:30";
$TripEnd = "03:30";
echo calc($TripStart, $TripEnd);

Open in new window

Dan-LLAuthor Commented:
Many Many Thanks for that, It works fantastic, I am very slightly over the moon, thanks once again.
Dan-LLAuthor Commented:
Thanks so much, gave you the bonus points as promised for a fast and in detailed reply... Thank You
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.