ibanez270dx Posted July 31, 2006 Share Posted July 31, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/16139-subtracting-times/ Share on other sites More sharing options...
nethnet Posted July 31, 2006 Share Posted July 31, 2006 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) Quote Link to comment https://forums.phpfreaks.com/topic/16139-subtracting-times/#findComment-66615 Share on other sites More sharing options...
Caesar Posted July 31, 2006 Share Posted July 31, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16139-subtracting-times/#findComment-66617 Share on other sites More sharing options...
Drumminxx Posted July 31, 2006 Share Posted July 31, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/16139-subtracting-times/#findComment-66621 Share on other sites More sharing options...
Caesar Posted July 31, 2006 Share Posted July 31, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/16139-subtracting-times/#findComment-66622 Share on other sites More sharing options...
nethnet Posted July 31, 2006 Share Posted July 31, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16139-subtracting-times/#findComment-66625 Share on other sites More sharing options...
ibanez270dx Posted July 31, 2006 Author Share Posted July 31, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/16139-subtracting-times/#findComment-66685 Share on other sites More sharing options...
kenrbnsn Posted August 1, 2006 Share Posted August 1, 2006 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:minutesKen Quote Link to comment https://forums.phpfreaks.com/topic/16139-subtracting-times/#findComment-66788 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.