subtracting times

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

subtracting times

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

Post 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?
User avatar
mojeIme
Forum Newbie
Posts: 22
Joined: Sat Jul 29, 2006 8:58 am

Post by mojeIme »

Does it have to count considering the days or just in one day? (without date)
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

Post 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.
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

viewtopic.php?t=52658 may be of interest.
Post Reply