PHP Function to Convert Seconds into Days, Hours and Minutes

Date September 7, 2009

I needed a function for CalFeed to turn Seconds Until into Days, Hours and Minutes. I found an existing function on the web and made some modifications to it.

Here’s the function…


function secondsToWords($seconds)
{
/*** number of days ***/
$days=(int)($seconds/86400);
/*** if more than one day ***/
$plural = $days > 1 ? 'days' : 'day';

if ($days > 0) {
$day_string = "$days $plural ";
}

/*** number of hours ***/
$hours = (int)(($seconds-($days*86400))/3600);

if ($days != 0 or $hours != 0) {
if ($hours > 1) {
$hour_string = "$hours hrs ";
} else {
$hour_string = "$hours hr ";
}

}

/*** number of mins ***/
$mins = (int)(($seconds-$days*86400-$hours*3600)/60);

if ($mins != 0) {
$min_string = "$mins mins";
}

/*** return the string ***/
return "$day_string$hour_string$min_string";
}

If I can find a link to the original function, I will post that later.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Ma.gnolia
  • NewsVine
  • Propeller
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Simpy
  • Sk-rt
  • TailRank
  • Technorati
  • TwitThis

Leave a Reply

You must be logged in to post a comment.