There is no reason to do it in a one-liner unless its the easiest and most readable way. Don't sacrifice readable code for shorter code.
The below function is the simplest and easiest way to convert seconds into a minute:seconds
format. The reason for not using date("H:i", $s)
(which would yield the same result) is that it will not scale well, and return incorrect results if $s
ever gets high.
Divide by 60, floor it - that will get you the minutes. Multiply it by 60 and subtract it from $s
, and you have your seconds.
function fmtMSS($s){
$minutes = floor($s/60);
$seconds = $s-$minutes*60;
return "$minutes:$seconds";
};
echo fmtMSS(235); // 3:55
Live demo
The reason your original code doesn't work, is because you're using +
to join strings (as you would in JavaScript). However, in PHP, one uses .
to contact two strings. The logic also seems to be a bit incorrect.