views:

141

answers:

3

Hi,

I have an hour selection drop down 0-23 and minutes selection drop down 0-59 for Start time and End time respectively (so four controls).

I'm looking for an algorithm to calculate time difference using these four values.

Since they're not stored in fancy date/time selection controls, I don't think I can use any standard date/time manipulation functions.

How do I calculate the difference between the two times?

+5  A: 

This pseudo-code gives you the algorithm to work out the difference in minutes.

It assumes that, if the start time is after the end time, the start time was actually on the previous day.

startx = starthour * 60 + startminute
endx = endhour * 60 + endminute
duration = endx - startx
if duration < 0:
    duration = duration + 1440

The startx and endx values are the number of minutes since midnight.

This is basically doing:

  • Get number of minutes from start of day for start time.
  • Get number of minutes from start of day for end time.
  • Subtract the former from the latter.
  • If result is negative, add number of minutes in a day.

Don't be so sure though that you can't use date/time manipulation functions. You may find that you could easily construct a date/time and calculate differences with something like:

DateTime startx = new DateTime (1,1,2010,starthour,startminute,0);
DateTime endx   = new DateTime (1,1,2010,endhour  ,endminute  ,0);
Integer duration = DateTime.DiffSecs (endx,startx) / 60;
if (duration < 0)
    duration = duration + 1440;

although it's probably not needed for your simple scenario. I'd stick with the pseudo-code I gave above unless you find yourself doing some trickier date/time manipulation.


If you then want to turn the duration (in minutes) into hours and minutes:

durHours = int(duration / 60)
durMinutes = duration % 60 // could also use duration - (durHours * 60)
paxdiablo
Thanks Pax, T1=5: 50 T2=8:30startx= 5*60 + 50 = 350 endx=8*60+30 = 510 duration = 510- 350 = 160 = 2 hr 40 mins correct should be 2 hr 50 minsAm I missing anything ?
Rishi
Yes, some mathematical ability in the sexagesimal system :-) From 5:50 to 8:30 in two hours and _forty_ minutes. Two hours takes you from 5:50 to 7:50, ten minutes to 8:00 then thirty minutes to 8:30. The ten and the thirty make forty minutes, not fifty. I'm not sure where this insistence is coming from that it's 2:50 duration. Is there something _we're_ missing?
paxdiablo
paxdiablo,No you are not missing anything. Still I'm thinking why did I put 50.. Something went wrong to my mathematical intellect..
Rishi
A: 

First you need to check to see if the end time is greater than or equal to the start time to prevent any problems. To do this you first check to see if the End_Time_Hour is greater than Start_Time_Hour. If they're equal you would instead check to see if End_Time_Min is greater than or equal to Start_Time_Min. Next you would subtract Start_Time_Hour from End_Time_Hour. Then you would subtract Start_Time_Min from End_Time_Min. If the difference of the minutes is less than 0 you would decrement the hour difference by one and add the minute difference to 60 (or 59, test that). Concat these two together and you should be all set.

Aaron Hathaway
Thanks Aaron .Can you confirm I'm demonstrating correctly ,T1= 5:50T2=8:30h= 8 - 5 = 3m=30-50 = -20 since m is less than 0 so , h= 3-1 = 2 now add the minute difference to 60 , m = 60+20 = 80 result , 2:80 ?
Rishi
I meant add the minutes difference but leaving the negative sign. So 30-50 = -20... m = 60 + -20 = 40. Or you could just subtract the absolute value :D
Aaron Hathaway
Aaron, Thanks.. but it will still come 2:40 , but correct one should be 2:50 :P
Rishi
Are you sure? I believe 8:30 minus 5:50 is 2 hours and 40 minutes or 2:40...
Aaron Hathaway
Why the hell I've written 50 instead of 40 :( ..Sorry Aaron something came into my mind and such type mismatch happened..
Rishi
Cool, well did this help you out then?
Aaron Hathaway
Aaron, There is one more scenario which I'm thinking.. Let's say Start time is 23:59 and end time is 1:30 ..
Rishi
Hmmm...well I guess the constraint that the start time is before the end time doesn't apply then? I figured so since there's no date field. Anyways, to do this just check to see if the end time is greater than the start time. If so you could add 24 to the end time's hour and proceed as normal. Example given:Hour_Diff = (End_Hour + 24) - Start_Hour = (1 + 24) - 23 = 25 - 23 = 2Min_Diff = End_Min - Start_Min = 30 - 59 = -29... < 0 so add 60 to min and decrement Hour_Diff...Min_Diff = -29 + 60 = 31 and Hour_Diff = 1Concat to get 1:31 for total difference.
Aaron Hathaway
A: 
$start_time_hr = 5;

$start_time_mi = 50;

$end_time_hr = 8;

$end_time_mi = 30;

$diff = (($end_time_hr*60)+$end_time_mi) - (($start_time_hr*60)+$start_time_mi);

$diff_hr = (int)($diff / 60);

$diff_mi = (int)($diff) - ($diff_hr*60);

echo $diff_hr . ':' . $diff_mi;
pawan