Page 1 of 1

subtracting times

Posted: Mon Jul 31, 2006 3:23 pm
by ibanez270dx
Hi,
There is a form on my site that requires that the user types in the start time and end time of down time. If this sounds confusing, check it out: This system logs the downtime for aircraft at my company, and the user must be able to type in the time the aircraft became out of service, as well as the time the aircraft became back in service. I need the PHP to calculate the difference and insert that number (in hours) into my DB, but I'm not sure how to do this since time goes doesn't work in intervals of 100, but 60. Is there some sort of built-in fuction to deal with this? What can I do to resolve this problem?

Thanks,
- Jeff

Posted: Mon Jul 31, 2006 3:33 pm
by pickle
Look at the date() function. You can subtract timestamps to arrive at the number of seconds between the two stamps. It's simple math (unless you're in a leap year ;)) to get the hours from there.

Posted: Mon Jul 31, 2006 3:34 pm
by feyd
If the times were in unix timestamps (integers) then the basic math operators will perform what you wish. If they are in another format, your database may be able to tell you the difference or you may need to convert them to unix timestamps (or similar) for php to be able to do the math.

Posted: Mon Jul 31, 2006 3:40 pm
by ibanez270dx
how would I make them timestamps? The user has to input the times because the logs are generally made a few hours or a day after the actual downtime. I was hoping to be able to let the user enter the time like... 13:25 (in one box) to 15:50 (in another box).

Any ideas?

Posted: Mon Jul 31, 2006 3:42 pm
by mojeIme
Does it have to count considering the days or just in one day? (without date)

Posted: Mon Jul 31, 2006 3:48 pm
by ibanez270dx
all I need is the hours - from a start time to finish time. Usually the maintenance is completed in one day, thus I wouldn't have to deal with the days. I would like it to count days if possible, just in case that scenario ever did come about.

I was thinking the times would be entered via a regular old text input box.

Posted: Mon Jul 31, 2006 5:28 pm
by ibanez270dx
I ended up not using the day thing, but I put the times in Unix Timestamps using this method:

Code: Select all

$date_a = strtotime($dwntime_hrs1);
$date_b = strtotime($dwntime_hrs2);

$dthrs = ($date_b-$date_a)/3600;
however, my output is in decimal form - EX:, I put in 08:00 to 13:45 and got 5.75 for the difference. Is there any way to put this into a time standard format? Such as 5:45 or 5hrs and 45mins or something?

Thanks again,
- Jeff

Posted: Mon Jul 31, 2006 8:12 pm
by feyd
viewtopic.php?t=52658 may be of interest.