Page 1 of 1

Time Interval Calc Out By An Hr / Convert Time to Decimal

Posted: Mon Nov 03, 2003 7:05 pm
by dickey
Q1...
I'm using mktime to create a start and finish time.
I then subtract the start time from the finish time.
The interval as calculated is an hr short of the correct result.

Code: Select all

$start_dt = mktime($start_hr, $start_min, 0, $start_mth, $start_day, $start_yr);
echo date("F j, Y - g:i A", $start_dt); // outputs November 8, 2003 - 9:30 PM
$finish_dt = mktime($finish_hr, $finish_min, 0, $finish_mth, $finish_day, $finish_yr1);
echo date("F j, Y - g:i A", $finish_dt);// outputs November 9, 2003 - 2:45 AM

$interval = $finish_dt - $start_dt;
echo date("g:i", $interval); //outputs 4:15 and not 5:15 as required
Q2...
How do I convert the result obtained from subtracting a start (mktime result) from a finish (mktime result) to a decimal.
For example if the interval result is 5:15, I wish to calculate the decimal equivalent as 5.25 (for use in an invoicing app).

Any assistance will be most appreciated.

- Andrew

Posted: Tue Nov 04, 2003 3:07 am
by twigletmac
Q1: I get 5:15.

Q2: To get the number of hours difference as a decimal:

Code: Select all

// you've got the number of seconds, so divide by 60 (60 seconds in a 
// minute) and 60 again (60 minutes in a hour) to get the number of 
// hours difference
echo $interval_dec = $interval / 60 / 60;
Mac