Louis Shiggy Lombardi Louis Shiggy Lombardi - 1 year ago 141
PHP Question

Convert a string to seconds with PHP

I have string '6m15s' in PHP that I would like to convert in to time or ideally seconds. I am using the

function like:

$time = date('H:i:s', strtotime('6m15s'));
echo $time;

But all I get '01:00:00'. Is this possible to convert my string and if so what would be the best way to do so?

Answer Source

You could use substr():

$string = '6m15s';
$minute = substr($string, 0, 1);
$second = substr($string, 2, 2);
echo date('H:i:s', strtotime("00:$minute:$second"));
// outputs 00:06:15
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download