Louis Shiggy Lombardi Louis Shiggy Lombardi - 4 months ago 15x
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?


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