Convert Seconds to String

This function converts seconds to an easily readable string

<?php
function secondsToString($secs) 
{
	$r=0;
	if($secs>=86400){$days=floor($secs/86400);$secs=$secs%86400;$r=$days.' day';if($days<>1){$r.='s';}if($secs>0){$r.=', ';}}
	if($secs>=3600){$hours=floor($secs/3600);$secs=$secs%3600;$r.=$hours.' hour';if($hours<>1){$r.='s';}if($secs>0){$r.=', ';}}
	if($secs>=60){$minutes=floor($secs/60);$secs=$secs%60;$r.=$minutes.' minute';if($minutes<>1){$r.='s';}if($secs>0){$r.=', ';}}
	$r.=$secs.' second';if($secs<>1){$r.='s';}
	return $r;
}

//1 day, 22 hours, 35 minutes, 10 seconds
echo secondsToString(167710);
//268 days, 26 minutes, 18 seconds
echo secondsToString(23156778);
//03 minutes, 55 seconds
echo secondsToString(235);


?>

LEAVE A REPLY

Please enter your comment!
Please enter your name here

*