Re: convertion from string to time & substracting

From: Date: Fri, 07 Jun 2002 18:27:02 +0000
Subject: Re: convertion from string to time & substracting
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-101167@lists.php.net to get a copy of this message
Thank you very much Erik, I'll try it :) anyway, it would be nice to know what's wrong with the code I posted, why does it work with CHAR and not with TIME types? juaid From: "Erik Price" <pricee@hhbrown.com> > > First of all, if you're storing time then you're better off using the > DATETIME column type. Even though it may take a bit more space than > CHAR(8), unless you absolutely need the ultimate in table optimization, > use DATETIME. It makes comparisons and manipulations like the one you > are trying to do much easier. (Because you can convert them to > UNIX_TIMESTAMP and then do your math on that, which is a piece of cake.) > > But to answer your question, here's what I would do: > > // $db contains database connection handle > $sql = "SELECT row FROM table WHERE where_clause"; > // attempt to pull data from DB > if (!$result = mysql_query($sql, $db)) { > die('Query failed.'); > } > > // assuming only one row is returned > $row = mysql_result($result, 0); > > $time_array = explode(':', $row); > $hours = $time_array[0]; > $minutes = $time_array[1]; > $seconds = $time_array[2]; > > // convert to timestamp so we can easily do math > $timestamp = mktime($hours, $minutes, $seconds, 0, 0, 0); > > Now all of your times are in timestamp form, and you can do what you > need to do.

« previous php.general (#101167) next »