Storing microtime in DB

$start_time = microtime(true);
...
..
.
$elapsed_time = microtime(true) - $start_time;

I need to store this elapsed time (I guess it probably looks like something 11.11111111111 ?) in db, what is the best MySQL field type to setup for this to store it?

Hi Nimasdj,

The timestamp is used to store a moment in time, not a duration of time.
May it help yours http://dev.mysql.com/doc/refman/5.1/en/datetime.html

I know, I need to store this duration of time, what is the best MySQL field type for such output?

why not echo the value - you’ll see what it looks like then

echo microtime(true);

looks something like this.

1447329872.0929

So, I believe for your use case, DECIMAL(14, 4) should do the trick.

Scott

FLOAT or DOUBLE depending on how much precision you need. But DECIMAL would also be fine - I once had a similar case to yours and I stored the script duration in a DECIMAL(5,3) column. This works fine and I have the numbers nicely formatted in the db (zero-padded to 3 decimal places).

1 Like

Ah…yes, my bad. DECIMAL(5,3) is correct.

Scott

I was googling how DOUBLE looks like, the only thing I found is that it is 8 bytes, can you give some explanation how much is maxmimum allowable value for DOUBLE and how does it look like? I know when integer increases, decimal reduces but please give some explanations. and which width would be good to use with DOUBLE(?) ?

https://dev.mysql.com/doc/refman/5.7/en/floating-point-types.html

FLOAT(5,3) or DOUBLE PRECISION(5,3)?

Scott

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.