Convert Seconds To Hours Minutes Seconds Words
Here is a PHP function that will convert seconds into hours, minutes and seconds. The function makes use of bcmod to do the modulo operations and with some simple strings, the result is returned. The demonstration uses the PHP time() function to get the number of seconds since EPOCH, but any interger will be converted to the hours minutes seconds interpretation.
<?php
/**
*
* @convert seconds to hours minutes and seconds
*
* @param int $seconds The number of seconds
*
* @return string
*
*/
function secondsToWords($seconds)
{
/*** return value ***/
$ret = "";
/*** get the hours ***/
$hours = intval(intval($seconds) / 3600);
if($hours > 0)
{
$ret .= "$hours hours ";
}
/*** get the minutes ***/
$minutes = bcmod((intval($seconds) / 60),60);
if($hours > 0 || $minutes > 0)
{
$ret .= "$minutes minutes ";
}
/*** get the seconds ***/
$seconds = bcmod(intval($seconds),60);
$ret .= "$seconds seconds";
return $ret;
}
?>
Example Usage
<?php
/*** time since EPOCH ***/
echo secondsToWords(time());
?>
Demonstration
Time since EPOCH
369112 hours 51 minutes 0 seconds
Support PHPRO.ORG
Search
PHPRO.ORG Poll
Warning: Participation in PHPRO.ORG polls may incorrectly lead you to believe your opinions matter.

RSS Feed




