# Re: [PHP-DB] Re: subtracting times (solution)

It took me about 30 mins but after 2 attempts i came up with a function that subtracts 2 times to give an answer in hours, to two decimal places.

thanks for your help the two people who responded when i posted the question, your suggestions were a bit out on a tangent from what i wanted. But they got me kinda thinkin about it.

the purpose of this is to calculate staffs payroll, by gettin their shift total of hours, this script assumes that a person will be starting work on one day, and finishing before 4am the next day.

here's the code if your interested

=================================================================

```<?
function subtract_times(\$time1, \$time2){```

```                \$time1array = split(":",\$time1);
\$time2array = split(":",\$time2);```

```                \$hours1 = ( ( \$time1array[0] ) * 60 );
\$minutes1 = \$time1array[1];```

```                \$hours2 = ( ( \$time2array[0] ) * 60 );
\$minutes2 = \$time2array[1];```

```                \$subtotal1 = (\$hours1 + \$minutes1);
\$subtotal2 = (\$hours2 + \$minutes2);```

if ( ("0" <= \$subtotal2) && ( \$subtotal2 <= "300") ){

```                        \$subtotal1 = ( 1440 - ( \$subtotal1 ) );
\$total = ( round ( ( (\$subtotal1 + \$subtotal2) / 60 ) , 2 ) );
}
else{```

\$subtotal = ( ( ( \$hours2 - \$hours1 ) ) + ( \$minutes2 - \$minutes1 ) );

```                        \$total = ( round ( ( (\$subtotal) / 60 ) , 2 ) ) ;
}```

return \$total;

}

```_________________________________________________________________
MSN Messenger - fast, easy and FREE! http://messenger.msn.co.uk```

```--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php```

```

```