Re: Subtraction of two units of time.
| From: | 1LT John W. Holmes | Date: | Thu, 06 Mar 2003 21:03:31 +0000 |
| Subject: | Re: Subtraction of two units of time. | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-26140@lists.php.net to get a copy of this message | ||
> I want to take away two times stored in the format "00:00:00" in a mysql
> database, i retrieve the times and take them away by using the following
>
> $total = $time1 - $time2 ;
>
> when i echo the total it is a whole number of the hours.. and does not
take
> the minutes into account, anyone have an idea of how to get an exact
answer
> to 2decimal places of the subtraction of the two times?
Read the manual. These two functions will probably come in handy (assuming
MySQL):
SEC_TO_TIME(seconds)
Returns the seconds argument, converted to hours, minutes, and seconds, as a
value in 'HH:MM:SS' or HHMMSS format, depending on whether the function is
used in a string or numeric context:
mysql> SELECT SEC_TO_TIME(2378);
-> '00:39:38'
mysql> SELECT SEC_TO_TIME(2378) + 0;
-> 3938
TIME_TO_SEC(time)
Returns the time argument, converted to seconds:
mysql> SELECT TIME_TO_SEC('22:23:00');
-> 80580
mysql> SELECT TIME_TO_SEC('00:39:38');
-> 2378So... SELECT
SEC_TO_TIME(TIME_TO_SEC(column1)-TIME_TO_SEC(column2)) AS Difference
...---John Holmes...