Jump to content

subtracting times


ibanez270dx

Recommended Posts

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
Link to comment
Share on other sites

Subtracting times based on clock display (10:24 AM and 4:02 PM, for example) is very tedious, ineffective, and highly prone to error.  Look into UNIX timestamps instead.  Once you convert your clock times into a UNIX timestamp, you can just subtract the older time from the newer time, returning a UNIX timestamp in itself.  Then, you can convert this timestamp into hours of downtime.  Look into the following functions:

[url=http://us2.php.net/manual/en/function.variant-date-to-timestamp.php]variant_date_to_timestamp()[/url]
&
[url=http://us2.php.net/manual/en/function.variant-date-from-timestamp.php]variant_date_from_timestamp()[/url]

Hope this helps :)

-nethnet

(Edit: fixed links)
Link to comment
Share on other sites

Store your dates/time as timestamps in the database. Then when you access the information from your database, you can format them any which way you want. it also makes it easy to work with.

Here is an example I showed someone recently in regards to converting formatted dates, and then subtracting those dates.

[code]<?php
 
$date_a = strtotime($dateone);
$date_b = strtotime($datetwo);

$days = ($date_b - $date_a) / (60*60*24);

echo number_format($days);

?>[/code]

Of course you'd have to define those dates in question and if they are already formatted, convert them to timestamps. Hope that helps!

Note: This will subtract "days" but, the same thing can be done to subtract hours...with some slight modification.

Link to comment
Share on other sites

You have some good examples already so for what its worth here's another

[code]
$start_time = mktime(4,15); //start time is 4:15
$end_time = mktime(4,30); //end time is 4:30

$total_time = $end_time-$start_time;

echo 'total minutes: ' .$total_time/60;
[/code]
Link to comment
Share on other sites

[quote author=nethnet link=topic=102483.msg406751#msg406751 date=1154378229]
Subtracting times based on clock display (10:24 AM and 4:02 PM, for example) is very tedious, ineffective, and highly prone to error.
[/quote]

Well yes...it is easier if you not only store the time...but the date as well. So your "down time" can be January 20, 2007 4:06 PM. (I would store it as a timestamp though) After storing it, you can display only the time...or the diference in hours...or whatever you like.
Link to comment
Share on other sites

That's what I'm saying.  When people on his site enter the date/time of the start and end of the down time, they aren't going to be entering timestamps, they are going to be entering it as a typical person would.  It's easy then to convert that to variant, and then just use timestamps.  Any method that works is good, there is more than one solution.
Link to comment
Share on other sites

Hi,
Thanks for all the great help!!

I ended up putting the times in Unix Timestamps using this method:

[code]
$date_a = strtotime($dwntime_hrs1);
$date_b = strtotime($dwntime_hrs2);

$dthrs = ($date_b-$date_a)/3600;
[/code]

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
Link to comment
Share on other sites

Try this little demo script:
[code]<?php
$now = time();
$past = strtotime('july 31, 2006 1:12 am');
$midn = strtotime('july 31, 2006 00:00');
$dif = $now - $past;
echo date('G:i',$midn + $dif).'<br>';
echo floor($dif/3600) . ':' . floor(($dif % 3600)/60);
?>[/code]

Both echo statements at the end will show hours:minutes

Ken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.