Re: Subtraction of two units of time.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


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


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux