Jump to content


Photo

subtracting times


  • Please log in to reply
7 replies to this topic

#1 ibanez270dx

ibanez270dx
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 31 July 2006 - 08:24 PM

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

#2 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 31 July 2006 - 08:37 PM

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:

variant_date_to_timestamp()
&
variant_date_from_timestamp()

Hope this helps :)

-nethnet

(Edit: fixed links)
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#3 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 31 July 2006 - 08:37 PM

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.

<?php
  
 $date_a = strtotime($dateone);
 $date_b = strtotime($datetwo);
 
 $days = ($date_b - $date_a) / (60*60*24);
 
 echo number_format($days);
 
?>

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.


PHP Ninja

#4 Drumminxx

Drumminxx
  • Members
  • PipPipPip
  • Advanced Member
  • 92 posts
  • LocationUSA

Posted 31 July 2006 - 08:42 PM

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

	$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;

:: gucci.com ::
:: blockbuster.com ::

--------------------

#5 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 31 July 2006 - 08:43 PM

Subtracting times based on clock display (10:24 AM and 4:02 PM, for example) is very tedious, ineffective, and highly prone to error.


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.
PHP Ninja

#6 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 31 July 2006 - 08:45 PM

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.
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#7 ibanez270dx

ibanez270dx
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 31 July 2006 - 10:27 PM

Hi,
Thanks for all the great help!!

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

$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

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 01 August 2006 - 01:11 AM

Try this little demo script:
<?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);
?>

Both echo statements at the end will show hours:minutes

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users