johnny_s johnny_s - 2 months ago 5
MySQL Question

PHP Convert String to Duration

Using PHP, MySQLi.

I am selecting a string from a MySQL database and saving it to a variable called

$queryTime
, I'd like to modify how this string is displayed on screen. When I
print_r($queryTime);
I see;


01:25:24


When I
var_dump($queryTime)
I see;


string(8) "01:25:24"


Is there any way I can display the string in a time duration format - something like;


1 hour, 25 minutes, 24 seconds


Presumably because the database field is stored as a
varchar
and not a
time-stamp
things are more complicated? Unfortunately I don't have access to modify the database.

Any help is appreciated.

Answer

You could simple work on that string, if the format is always the same:

$queryTime = '01:25:24';

$parts = explode(':', $queryTime);
$parts[0] .= ' hours';
$parts[1] .= ' minutes';
$parts[2] .= ' seconds';

//01 hours, 25 minutes, 24 seconds
echo implode(', ', $parts);