Page 1 of 1

Using "time" var from database

Posted: Thu Feb 05, 2004 10:14 am
by Bill H
My data is stored as "time" columns in the database and returns in the array as strings in the form of "00:00:00". (At least it appears to, PHP is so loosely typed one cannot always be absolutely certain what one is looking at.) Anyway...

Are there any PHP functions for manipulating that data? Like subtracting a start time from a finish time to establish an elapsed time. If it were a "datetime" column I could ise the date() functionality, but I've checked in PHP docs and MySQL docs and done a search in these forums (fora?) and I cannot find anything similar for the "time" string.

Similarly with numbers stored as type "decimal" in MySQL. I know they are stored as strings, but I assume I can convert them simply enough by doing

Code: Select all

<?php
$Amount = 0.00 + $Row['Amount'];
?>
and then I have it in numeric form. I will experiment to confirm that as soon as I get back from a client call later today, but that clearly isn't going to fly for the time data.
:?:

Re: Using "time" var from database

Posted: Thu Feb 05, 2004 11:26 am
by Weirdan
Bill H wrote:Are there any PHP functions for manipulating that data? Like subtracting a start time from a finish time to establish an elapsed time. If it were a "datetime" column I could ise the date() functionality, but I've checked in PHP docs and MySQL docs and done a search in these forums (fora?) and I cannot find anything similar for the "time" string.
in MySQL query you can use time_to_sec and sec_to_time to perform such a calculations.
to get difference between two moments (measured in seconds) in php you can use strtotime:

Code: Select all

echo abs(strtotime("00:03:12")-strtotime("00:06:02"));

Posted: Thu Feb 05, 2004 8:18 pm
by Bill H
Hmmm. My reading of the strtotime() function was that it required a date input. After reading your reply and re-reading the documentation it would seem that the time string argument will generate a Unix timestamp of "now" plus that time which can then be manipulated by simple arithmetic.

Cool: precisely what I was looking for and had myopically missed. Thanks for the input.
:oops:

Posted: Fri Feb 06, 2004 3:06 am
by twigletmac
Using MySQL for manipulating the dates would be my choice, saves a lot of PHP code:
http://www.mysql.com/doc/en/Date_and_ti ... tions.html

Mac