johnny_s johnny_s - 1 year ago 86
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

, I'd like to modify how this string is displayed on screen. When I
I see;


When I
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
and not a
things are more complicated? Unfortunately I don't have access to modify the database.

Any help is appreciated.

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download