Seconds to String

By Daveoh on Mar 04, 2009

This function will return the duration of the given time period in days, hours, minutes and seconds.


secstostr(1234567) would return "14 days, 6 hours, 56 minutes, 7 seconds"

function secstostr($secs) {
  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;


Sign in to comment.
Korvin   -  Aug 13, 2009

agreed, I always use to do this in the script, never put it in a function =D

Hawkee   -  Mar 04, 2009

Nice, although I don't think the commas between each duration are necessary.

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.