Using "time" var from database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Using "time" var from database

Post 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.
:?:
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Using "time" var from database

Post 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"));
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply